<?php

namespace app\admin\controller\kuyou;

use app\common\controller\Backend;
use think\Db;
use Exception;
use think\Session;
/**
 * 销货单
 *
 * @icon fa fa-circle-o
 */
class GoodsSales extends Backend
{
    protected $noNeedRight = ['checkPurchase','getSuppliers','getUnits','getUnit','formatInfo2','getBranch','getTime','getReceivingAddress'];
    /**
     * GoodsSales模型对象
     * @var \app\admin\model\kuyou\GoodsSales
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set',$setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $this->model = new \app\admin\model\kuyou\GoodsSales;
        $this->view->assign("purchaseTypeList", $this->model->getPurchaseTypeList());
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("checkStatusList", $this->model->getCheckStatusList());
        $this->view->assign("examineStatusList", $this->model->getExamineStatusList());

        $this->view->assign("suppliersList", $this->model->getSuppliersList());
        $this->assignconfig("suppliersList", $this->model->getSuppliersList());
        $this->assignconfig("expendTypeList", $this->model->getExpendTypeList());
        $this->view->assign("storesList", $this->model->getStoresList());
        $this->assignconfig("storesList", $this->model->getStoresList());

        $this->view->assign("goodsList", $this->model->getGoodsList());
        $this->assignconfig("goodsList", $this->model->getGoodsList());

        $this->view->assign("adminList", $this->model->getAdminList());
        $this->view->assign("accountsList", $this->model->getAccountsList());

        $this->view->assign("customers", $this->model->getCustomersList());
        $this->view->assign("saleList", $this->model->getSaleList());

        $accountsList = $this->model->getAccountsList();
        foreach ($accountsList as $accountsList_k=>$accountsList_v){
            if($accountsList_v['is_default']==1){
                $this->view->assign("accountsDefault", $accountsList_v['id']);
                $this->assignconfig("accountsDefault", $accountsList_v['id']);
            }
        }
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sort = 'purchase_time';$order='DESC';
            $list = $this->model
                ->with(['customers','preparation','examine'])
                ->where($where)
                ->where(['goods_sales.order_type'=>'1'])
                ->order($sort, $order)
                ->order('purchase_code', 'DESC')
                ->paginate($limit);

            foreach ($list as $row) {
                $row->getRelation('customers')->visible(['customer_name']);
                $row->getRelation('preparation')->visible(['username']);
                $row->getRelation('examine')->visible(['username']);
            }
            foreach ($list as $list_k=>$list_v){
                if(isset($list_v['print_num']) && empty($list_v['print_num'])){
                    $list[$list_k]['print_num'] = 0;
                }
                $purchase_id = Db::name('kuyou_goods_saleexc')->where(['purchase_id'=>$list_v['id']])->column('exchange_id');
                $purchase = Db::name('kuyou_goods_sales_exchange')->where(['id'=>['in',$purchase_id]])->column('purchase_code');
                $list[$list_k]['returns_order_id'] = implode(',',$purchase);

                if(isset($list_v['third_id']) && $list_v['third_id']>0){
                $list[$list_k]['preparation']['username'] = '三方订单';
                    $list[$list_k]['examine']['username'] = '三方订单';
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add($ids = null,$order_id=null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                (new \app\admin\controller\kuyou\Checkout())->checkOutTime($params['purchase_time']);
                if(empty($params['customers_id'])){
                    $this->error(__('Please select customer'));
                }
                foreach ($params['goods_code'] as $k=>$v){
                    if(!empty($v)) {//有产品编号
                        $status = true;
                    }
                }
                if(!isset($status)){
                    $this->error(__('Please fill in the detailed data'));
                }
                $examine= Db::name('kuyou_sets')->where(['name'=>'examine'])->value('is_checked');
                $params = $this->preExcludeFields($params);

                if(empty($params['purchase_code'])){
                    $this->error(__('Please fill in the document number'));
                }

                $admin = Session::get('admin');
                if($params['now_pay_money']>0){
                    if(empty($params['account'])){
                        $this->error(__('Please select a settlement account'));
                    }
                    $all_now_pay_money = $now_pay_money = trim($params['now_pay_money']);
                }else{
                    $all_now_pay_money = $now_pay_money = 0;
                }

                $set= Db::name('kuyou_sets')->where(['name'=>'serial'])->value('is_checked');
                $batch= Db::name('kuyou_sets')->where(['name'=>'batch'])->value('is_checked');
                $auxiliary_status= Db::name('kuyou_sets')->where(['name'=>'auxiliary'])->value('is_checked');
                $data = [];
                $number = 0;$all_money = 0;$purchase_order_id = '';$tax_money=0;$money_tax_price=0;
                foreach ($params['goods_code'] as $k=>$v){
                    if(!empty($v)){//有产品编号
                        $number += (float)$params['number'][$k];
                        $all_money += (float)$params['all_money'][$k];
                        $tax_money += (float)$params['tax_money'][$k];
                        $money_tax_price += (float)$params['money_tax_price'][$k];

                        if(!empty($params['purchase_order_id'][$k])){
                            $purchase_order_id = $params['purchase_order_id'][$k];
                        }

                        if(!isset($params['stores_id'][$k]) || empty($params['stores_id'][$k])){
                            $this->error(__('Please select warehouse'));
                        }

                        unset($pay_money);unset($pay_status);
                        if($now_pay_money>0){
                            if($now_pay_money-$params['money_tax_price'][$k]>=0){
                                $pay_status = 1;
                                $pay_money = $params['money_tax_price'][$k];
                                $now_pay_money -= $pay_money;
                            }else{
                                $pay_status = 2;
                                $pay_money = $now_pay_money;
                                $now_pay_money -= $now_pay_money;
                            }
                        }

                        $goods = Db::name('kuyou_goods')->where(['goods_code'=>$v])->field('id,serial,batch,auxiliary')->find();
                        $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$params['batch_id'][$k]])->find();
                        if ($goods['auxiliary']==1 && empty($params['goods_attr'][$k]) && $auxiliary_status==1){
                            return ['status'=>500,'msg'=>__('Please select a product').$v.__('attribute')];
                        }
                        $goods_batch_info = [];
                        if($goods['batch']==1 && $batch==1){
                            if(empty($params['batch'][$k])){
                                $this->error(__('Please select the production batch'));
                            }
                            if(empty($batch_data)){
                                $this->error(__('Production batch does not exist'));
                            }
                            $goods_batch_info = [
                                'batch_id'    => isset($batch_data['id'])?$batch_data['id']:'',
                                'goods_id'    => $goods['id'],
                                'goods_code'  => $params['goods_code'][$k],
                                'attribute_id'=>isset($params['goods_attr'][$k])?$params['goods_attr'][$k]:0,
                                'goods_unit'  =>  (int)$params['goods_unit'][$k],
                                'stores_id'   => $params['stores_id'][$k],
                                'stock'       => (float)$params['number'][$k],
                                'number'      => (float)$params['number'][$k],
                                'batch_name'  => isset($batch_data['batch_name'])?$batch_data['batch_name']:'',
                                'manufacture' => strtotime($params['manufacture'][$k]),
                                'guarantee'   => $params['guarantee'][$k],
                                'validity'    => strtotime($params['validity'][$k]),
                                'status'      => '0',
                                'type'        => 2,
                                'createtime'  => time(),
                                'updatetime'  => time(),
                            ];
                        }

                        $serial_code = [];
                        if(isset($params['period_period'][$k]) && $goods['serial']==1 && $set==1){
                            if(empty($params['period_period'][$k])){
                                $this->error(__('Please enter the product serial number'));
                            }
                            $period_period = unserialize($params['period_period'][$k]);
                            foreach ($period_period as $period_period_k=>$period_period_v){
                                $serial_code[] = $period_period_v['serial'];
                            }
                            $where = [];
                            $where['serial'] = ['in',$serial_code];
                            if(!empty($params['purchase_id'][$k])){
                                $where['purchase_code'] = $params['purchase_id'][$k];
                            }
                            $goods_serial = Db::name('kuyou_goods_serial')->where($where)->column('serial');
                            if(empty($goods_serial)){
                                $this->error(__('The product serial number does not exist'));
                            }
                            foreach ($serial_code as $serial_code_k=>$serial_code_v){
                                if(!in_array($serial_code_v,$goods_serial)){
                                    $this->error($serial_code_v.__('The product serial number does not exist'));
                                }
                            }
                            $goods_serials = Db::name('kuyou_goods_serial')->where(['status'=>['in',[2,3]],'serial'=>['in',$serial_code]])->column('serial');
                            if(!empty($goods_serials)){
                                $goods_serials = implode(',',$goods_serials);
                                $this->error(__('Product serial number').$goods_serials.'已经待出库，不可重复出库');
                            }
                        }

                        if(!empty($params['info_id'][$k])){
                            $unit_id_order = Db::name('kuyou_goods_sales_order_info')->where(['id'=>$params['info_id'][$k]])->value('goods_unit');
                            if(!empty($unit_id_order) && $unit_id_order!=$params['goods_unit'][$k]){
                                $this->error('选择的商品单位与源销货订单不一致，保存失败');
                            }
                        }

                        $data[] = [
                            'purchase_code'     =>  $params['purchase_code'],
                            'purchase_type'     =>  $params['purchase_type'],
                            'purchase_time'     =>  strtotime($params['purchase_time']),
                            'customers_id'      =>  (int)$params['customers_id'],//客户
                            'sale_user'         =>  (int)$params['sale_user'],//销售员
                            'receiving_address' =>  $params['receiving_address'],

                            'goods_code'        =>  $v,//商品信息
                            'goods_note'        =>  $params['goods_note'][$k],
                            'goods_unit'        =>  (float)$params['goods_unit'][$k],

                            'stores_id'         =>  $params['stores_id'][$k],
                            'allow_stock'       =>  (float)$params['allow_stock'][$k],
                            'order_info_id'     =>  (float)$params['info_id'][$k],
                            'number'            =>  (float)$params['number'][$k],
                            'default_unit'      =>  (float)$params['default_unit'][$k],
                            'basic_nmber'       =>  (float)$params['basic_nmber'][$k],
                            'unit_price'        =>  (float)$params['unit_price'][$k],
                            'cost_price'        =>  0,
                            'discount'          =>  (float)$params['discount'][$k],
                            'dis_money'         =>  (float)$params['dis_money'][$k],
                            'all_money'         =>  (float)$params['all_money'][$k],
                            'remark'            =>  $params['remark'][$k],

                            'goods_attr'        =>  (float)$params['goods_attr'][$k],
                            'tax_included'      =>  (float)$params['tax_included'][$k],
                            'tax_rate'          =>  (float)$params['tax_rate'][$k],
                            'tax_money'         =>  (float)$params['tax_money'][$k],
                            'money_tax_price'   =>  (float)$params['money_tax_price'][$k],

                            'batch_id'          => isset($batch_data['id'])?$batch_data['id']:'',
                            'batch_name'        => isset($batch_data['batch_name'])?$batch_data['batch_name']:'',
                            'manufacture'       => strtotime($params['manufacture'][$k]),
                            'guarantee'         => $params['guarantee'][$k],
                            'validity'          => strtotime($params['validity'][$k]),

                            'preferential_rate' =>  (float)$params['preferential_rate'],
                            'preferential_money'=>  (float)$params['preferential_money'],
                            'real_money'        =>  (float)$params['real_money'],
                            'purchase_money'    =>  (float)$params['purchase_money'][$k],
                            'preparation_id'    =>  $admin['id'],
                            'operation_id'      =>  $admin['id'],

                            'pay_status'        =>  isset($pay_status)?$pay_status:'0',
                            'pay_money'         =>  isset($pay_money)?$pay_money:0,
                            'account'           =>  isset($pay_status)?$params['account']:0,

                            'purchase_order_id' =>  $params['purchase_order_id'][$k],

                            'createtime'        => time(),
                            'updatetime'        => time(),
                            'period'            =>  $params['period_period'][$k]===''?null:$params['period_period'][$k],
                            'serial_code'       => $serial_code,
                            'goods_batch_info'  => $goods_batch_info,
                            'user_order_info_id'     => isset($params['user_order_info_id'][$k])?$params['user_order_info_id'][$k]:'',
                        ];
                    }
                }
                $preferential_money = (float)$params['preferential_money'];
                if(!empty($data)){
                    if($all_now_pay_money>0){
                        if($all_now_pay_money+$preferential_money-$money_tax_price>=0){
                            $pay_status = 1;
                            $pay_money = $money_tax_price-$preferential_money;
                        }else{
                            $pay_status = 2;
                            $pay_money = $all_now_pay_money;
                        }
                    }

                    $row = [
                        'purchase_code'     =>  $params['purchase_code'],
                        'purchase_type'     =>  $params['purchase_type'],
                        'purchase_time'     =>  strtotime($params['purchase_time']),
                        'customers_id'      =>  (int)$params['customers_id'],//客户
                        'sale_user'         =>  (int)$params['sale_user'],//销售员
                        'receiving_address' =>  $params['receiving_address'],
                        'customers_money'   =>  (float)$params['customers_money'],

                        'number'            =>  $number,
                        'all_money'         =>  $all_money,

                        'tax_money'         =>  $tax_money,//税
                        'money_tax_price'   =>  $money_tax_price,//价税合计

                        'preferential_rate' =>  (float)$params['preferential_rate'],
                        'preferential_money'=>  (float)$params['preferential_money'],
                        'real_money'        =>  (float)$params['real_money'],
                        'purchase_money'    =>  (float)$params['costs'],
                        'note'              =>  $params['note'],
                        'preparation_id'    =>  $admin['id'],
                        'operation_id'      =>  $admin['id'],

                        'arrears'           =>  $params['arrears'],
                        'all_arrears'       =>  $params['add_arrears'],

                        'purchase_order_id' =>  $purchase_order_id,

                        'pay_status'        =>  isset($pay_status)?$pay_status:'0',
                        'pay_money'         =>  isset($pay_money)?$pay_money:0,
                        'account'           =>  isset($pay_status)?$params['account']:0,
                        'cost_data'         =>  isset($params['cost_data'])?$params['cost_data']:null,
                        'createtime'        => time(),
                        'updatetime'        => time(),
                    ];
                }

                if($this->model->where(['purchase_code'=>trim($params['purchase_code'])])->find()){
                    $this->error(__('The document number is already occupied'));
                }
                if($purchase_order_id){
                    $order_id = Db::name('kuyou_goods_sales_order')->where(['purchase_code'=>$purchase_order_id])->value('id');
                }

                //附件记录开始
                $enclosureLog = [];
                if(isset($params['enclosure']) && !empty($params['enclosure'])){//选择了原始单据
                    $enclosure = explode(',',$params['enclosure']);
                    $enclosurelist = Db::name('kuyou_enclosure')->where(['id'=>['in',$params['enclosure']]])->select();
                    if(sizeof($enclosurelist)!=sizeof($enclosure)){
                        $this->error('选择的原始单据有误，请重新选择');
                    }else{
                        foreach ($enclosure as $k=>$v){
                            $enclosureLog[] = [
                                'enclosure_id'  => $v,
                                'purchase_code' => $params['purchase_code'],
                                'type'          => '5',
                            ];
                        }
                    }
                    unset($params['enclosure']);
                }
                //附件记录结束

                $info_id = Db::name('kuyou_goods_sales_order_info')->where(['purchase_code'=>$params['purchase_code']])->column('id');
                $result = false;
                Db::startTrans();
                try {
                    if(!empty($row)){
                        $result = $this->model->insertGetId($row);
                    }

                    if(!empty($params['order_id'])){
                        Db::name('kuyou_user_ordersale')->insert([
                            'order_id' => $params['order_id'],
                            'sales_id' => $result
                        ]);
                    }

                    //添加附件记录
                    if(!empty($enclosureLog)){
                        foreach ($enclosureLog as $enclosureLog_k=>$enclosureLog_v){
                            $enclosureLog[$enclosureLog_k]['order_id'] = $result;
                        }
                        Db::name('kuyou_enclosure_log')->insertAll($enclosureLog);
                    }
                    //添加附件记录结束

                    if(!empty($info_id)){
                        Db::name('kuyou_goods_saleord_info')->where(['purchase_id'=>['in',$info_id]])->delete();
                    }
                    Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$params['purchase_code']])->delete();
                    foreach ($data as $data_k=>$data_v){
                        $goods_serial = $data_v['serial_code'];
                        unset($data_v['serial_code']);

                        $goods_batch_info = $data_v['goods_batch_info'];
                        unset($data_v['goods_batch_info']);

                        $user_order_info_id = $data_v['user_order_info_id'];unset($data_v['user_order_info_id']);
                        $purchase_id = Db::name('kuyou_goods_sales_info')->insertGetId($data_v);
                        if(!empty($user_order_info_id) && !empty($params['order_id'])){
                            Db::name('kuyou_user_ordersaleinfo')->insert([
                                'sales_id' => $result,
                                'orderinfo_id' => $user_order_info_id,
                                'salesinfo_id' => $purchase_id,
                            ]);
                        }

                        //批次
                        if($goods_batch_info){
                            $goods_batch_info['purchase_info_id'] = $purchase_id;
                            Db::name('kuyou_goods_batch_sale_info')->insert($goods_batch_info);
                        }

                        if(!empty($goods_serial)){
                            Db::name('kuyou_goods_serial')->where(['serial'=>['in',$goods_serial]])->update([
                                'status'=>'3',
                                'sale_id' => $purchase_id,
                                'sale_code' => $data_v['purchase_code']
                            ]);
                        }

                        if(!empty($data_v['order_info_id'])){
                            Db::name('kuyou_goods_saleord_info')->insert([
                                'order_id'=>$data_v['order_info_id'],
                                'purchase_id'=>$purchase_id,
                                'number'=>$data_v['number'],
                                'money'=>$data_v['pay_money'],
                                'all_money'=>$data_v['all_money'],
                                'tax_rate'=>$data_v['tax_rate'],
                                'tax_money'=>$data_v['tax_money'],
                                'money_tax_price'=>$data_v['money_tax_price']
                            ]);
                        }
                    }
                    if(!empty($purchase_order_id)){
                        if($result){
                            Db::name('kuyou_goods_saleord')->insert(['order_id'=>$order_id,'purchase_id'=>$result]);
                        }
                    }
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if (isset($result) && $result !== false) {
                    if($examine!=1){
                        try {
                            $exa = $this->examine($result,1);
                        } catch (Exception $e) {
                        }
                        if(!isset($exa) || $exa['status']!=1){
                            $exa = $this->examine($result,1);
                            try {
                                $this->del($result);
                            } catch (Exception $e) {
                            }
                            $this->error($exa['msg']);
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $code = (new Goods())->getCodes('',11);
        $this->view->assign("code", $code);

        $row = Db::name('kuyou_goods_sales_order')->where(['id'=>$ids])->find();

        $last_customer = \think\Db::name('kuyou_sets')->where(['name'=>'customer'])->value('is_checked');
        $set_customer = '';
        if($last_customer){
            $set_customer = Db::name('kuyou_goods_sales')->where((new Goods())->setWhere())->order('id','DESC')->value('customers_id');
        }
        $address = Db::name('kuyou_customers')->where((new Goods())->setWhere(['id'=>$set_customer]))->value('getgoods_address');
        $last_read = \think\Db::name('kuyou_sets')->where(['name'=>'last_read'])->value('is_checked');
        $sale_user = '';
        if($last_read){
            $sale_user = Db::name('kuyou_goods_sales')->where((new Goods())->setWhere())->order('id','DESC')->value('sale_user');
        }

        $give_account = 0;$real_money = 0;$arrears = 0;$real_moneys = 0;
        if($row){
            $purchase_info = Db::name('kuyou_goods_sales_order_info')->where(['purchase_code'=>$row['purchase_code']])->select();
            foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                $purord_info = Db::name('kuyou_goods_saleord_info')->where(['order_id'=>$purchase_info_v['id']])->field('sum(number) as number,sum(money) as money')->select();
                $gp_number = isset($purord_info[0]['number'])?$purord_info[0]['number']:0;
                $real_money += isset($purord_info[0]['money'])?$purord_info[0]['money']:0;
                $real_moneys += $purchase_info_v['money_tax_price']-$purchase_info_v['preferential_money']-($gp_number)*$purchase_info_v['tax_included'];
                if($purchase_info_v['number']-$gp_number<=0){
                    unset($purchase_info[$purchase_info_k]);
                }else{
                    $unit = json_decode($this->getUnit($purchase_info_v['goods_code']),true);
                    foreach ($unit['unit'] as $unit_k=>$unit_v){
                        if($unit_v['id']==$purchase_info_v['goods_unit']){
                            $purchase_info[$purchase_info_k]['basic_nmber'] = ($purchase_info_v['number']-$gp_number)*$unit_v['basic_nmber'];
                        }
                    }
                    $purchase_info[$purchase_info_k]['unit'] = $unit;
                    $purchase_info[$purchase_info_k]['all_money'] = ($purchase_info_v['number']-$gp_number)*$purchase_info_v['unit_price'];
                    $purchase_info[$purchase_info_k]['money_tax_price'] = ($purchase_info_v['number']-$gp_number)*$purchase_info_v['tax_included'];
                    $purchase_info[$purchase_info_k]['tax_money'] = ($purchase_info_v['number']-$gp_number)*$purchase_info_v['tax_money']/$purchase_info_v['number'];
                    $purchase_info[$purchase_info_k]['dis_money'] = $purchase_info[$purchase_info_k]['all_money']*($purchase_info_v['discount'])/100;
                    $arrears += $purchase_info[$purchase_info_k]['all_money'] -= $purchase_info[$purchase_info_k]['dis_money'];
                    $purchase_info[$purchase_info_k]['purchase_number'] = $purchase_info_v['number']-$gp_number;
                    $goods = Db::name('kuyou_goods')->where(['goods_code'=>$purchase_info_v['goods_code']])->field('batch,serial')->find();
                    $purchase_info[$purchase_info_k]['goods'] = $goods;
                }
            }
            $give_account = Db::name('kuyou_customers')->where(['id'=>$row['customers_id']])->value('need_receive_account');
        }else{
            $row['customers_id'] = $set_customer;
            $row['receiving_address'] = $address;
            $row['sale_user'] = $sale_user;
        }
        $row['real_money'] = $real_moneys;
        $purchase_info = isset($purchase_info)?$purchase_info:[];
        $this->assignconfig("give_account", $give_account);
        $give_account = $give_account+$real_money+$row['real_money'];

        if(!empty($order_id)){
            $order = Db::name('kuyou_user_order')->where(['id'=>$order_id])->find();
            if($order){
                $purchase_info = [];
                $customers_id = Db::name('kuyou_user')->where(['id'=>$order['user_id']])->value('customers_id');
                $customers = Db::name('kuyou_customers')->where(['id'=>$customers_id])->find();
                $goods_list = Db::name('kuyou_user_orderinfo')->where(['order_id'=>$order['id']])
                    ->select();
                foreach ($goods_list as $goods_list_k=>$goods_list_v){
                    $goods = Db::name('kuyou_goods')->where(['id'=>$goods_list_v['goods_id']])->find();
                    $unit = json_decode($this->getUnit($goods['goods_code']),true);

                    $goods_list_v['basic_number'] = $goods_list_v['number'];
                    foreach ($unit['unit'] as $unit_k=>$unit_v){
                        if($unit_v['id']==$goods_list_v['goods_unit']){
                            $goods_list_v['basic_number'] = ($goods_list_v['number'])*$unit_v['basic_nmber'];
                        }
                    }

                    $purchase_info[] = [
                        'id' => '',
                        'unit'  => $unit,
                        'goods_code' => $goods['goods_code'],
                        'goods_note' => $goods['note'],
                        'goods_attr' => $goods_list_v['goods_attr'],
                        'goods_unit' => $goods_list_v['goods_unit'],
                        'stores_id'  => $goods['manager_warehouse_id'],
                        'allow_stock'=> $goods['inventory'],
                        'purchase_number' => $goods_list_v['number'],
                        'default_unit' => $goods['unit_id'],
                        'basic_nmber'=> $goods_list_v['basic_number'],
                        'unit_price' => $goods_list_v['price'],
                        'tax_included' => $goods_list_v['price'],
                        'discount'=> 0,
                        'dis_money' => 0,
                        'all_money' => $goods_list_v['money'],
                        'tax_rate'=> 0,
                        'tax_money' => 0,
                        'money_tax_price' => $goods_list_v['money'],
                        'purchase_money' => '',
                        'remark'=> '',
                        'purchase_code' => '',
                        'user_order_info_id' => $goods_list_v['id'],
                    ];
                }
                $row['customers_id'] = $customers_id;
                $row['receiving_address'] = $order['address'];
                $row['real_money'] = $order['order_price'];
                $row['order_id'] = $order['id'];
                $give_account = $customers['need_receive_account'] + $order['order_price'];
                $this->assignconfig("receiving_address", $row['receiving_address']);
            }
        }

        $this->view->assign("row", $row);
        $this->view->assign("purchase_info", $purchase_info);
        $this->view->assign("give_account", $give_account);

        //查询原始单据
        $enclosure_id = Db::name('kuyou_enclosure_log')->where(['order_id'=>$ids,'type'=>'4'])->column('enclosure_id');
        if(!empty($enclosure_id)){
            $this->assign('enclosure_count',sizeof($enclosure_id));
            $enclosure_id = implode(',',$enclosure_id);
        }else{
            $enclosure_id = '';
        }
        $this->assign('enclosure_ids',$enclosure_id);

        if(!empty($row['customers_id'])){
            $kuyou_customers = Db::name('kuyou_customers')->where(['id'=>$row['customers_id']])->field('id,customer_name')->find();
            $this->assignconfig("kuyou_customers", $kuyou_customers);
        }

        $this->assignconfig("suppliersList", $this->model->getSuppliersList());
        $this->assignconfig("expendTypeList", $this->model->getExpendTypeList());
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            (new \app\admin\controller\kuyou\Checkout())->checkOutTime($row['purchase_time']);
            $examine= Db::name('kuyou_sets')->where(['name'=>'examine'])->value('is_checked');
            if($row['examine_status']==1 && $examine==1){
                $this->error(__('Cannot edit approved data'));
            }
            if($row['check_status']==1){
                $this->error(__('Cannot edit data that has already been processed'));
            }
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                if(empty($params['customers_id'])){
                    $this->error(__('Please select customer'));
                }
                foreach ($params['goods_code'] as $k=>$v){
                    if(!empty($v)) {//有产品编号
                        $status = true;
                    }
                }
                if(!isset($status)){
                    $this->error(__('Please fill in the detailed data'));
                }
                $old_purchase_code = $params['old_purchase_code'];unset($params['old_purchase_code']);
                $info_id = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$old_purchase_code])->column('id');

                if(empty($params['purchase_code'])){
                    $this->error(__('Please fill in the document number'));
                }

                $admin = Session::get('admin');
                if($params['now_pay_money']>0){
                    if(empty($params['account'])){
                        $this->error(__('Please select a settlement account'));
                    }
                    $all_now_pay_money = $now_pay_money = trim($params['now_pay_money']);
                }else{
                    $all_now_pay_money = $now_pay_money = 0;
                }

                $set= Db::name('kuyou_sets')->where(['name'=>'serial'])->value('is_checked');
                $batch= Db::name('kuyou_sets')->where(['name'=>'batch'])->value('is_checked');
                $auxiliary_status= Db::name('kuyou_sets')->where(['name'=>'auxiliary'])->value('is_checked');
                $data = [];
                $number = 0;$all_money = 0;$tax_money=0;$money_tax_price=0;
                foreach ($params['goods_code'] as $k=>$v){
                    if(!empty($v)){//有产品编号
                        $number+=$params['number'][$k];
                        $all_money+=$params['all_money'][$k];
                        $tax_money += (float)$params['tax_money'][$k];
                        $money_tax_price += (float)$params['money_tax_price'][$k];

                        if(!isset($params['stores_id'][$k]) || empty($params['stores_id'][$k])){
                            $this->error(__('Please select warehouse'));
                        }

                        unset($pay_money);unset($pay_status);
                        if($now_pay_money>0){
                            if($now_pay_money-$params['money_tax_price'][$k]>=0){
                                $pay_status = 1;
                                $pay_money = $params['money_tax_price'][$k];
                                $now_pay_money -= $pay_money;
                            }else{
                                $pay_status = 2;
                                $pay_money = $now_pay_money;
                                $now_pay_money -= $now_pay_money;
                            }
                        }

                        $goods = Db::name('kuyou_goods')->where(['goods_code'=>$v])->field('id,serial,batch,auxiliary')->find();
                        $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$params['batch_id'][$k]])->find();
                        if ($goods['auxiliary']==1 && empty($params['goods_attr'][$k]) && $auxiliary_status==1){
                            return ['status'=>500,'msg'=>__('Please select a product').$v.__('attribute')];
                        }
                        $goods_batch_info = [];
                        if($goods['batch']==1 && $batch==1){
                            if(empty($params['batch'][$k])){
                                $this->error(__('Please select the production batch'));
                            }
                            if(empty($batch_data)){
                                $this->error(__('Production batch does not exist'));
                            }
                            $goods_batch_info = [
                                'batch_id'    => isset($batch_data['id'])?$batch_data['id']:'',
                                'goods_id'    => $goods['id'],
                                'goods_code'  => $params['goods_code'][$k],
                                'attribute_id'=>isset($params['goods_attr'][$k])?$params['goods_attr'][$k]:0,
                                'goods_unit'  =>  (int)$params['goods_unit'][$k],
                                'stores_id'   => $params['stores_id'][$k],
                                'stock'       => (float)$params['number'][$k],
                                'number'      => (float)$params['number'][$k],
                                'batch_name'  => isset($batch_data['batch_name'])?$batch_data['batch_name']:'',
                                'manufacture' => strtotime($params['manufacture'][$k]),
                                'guarantee'   => $params['guarantee'][$k],
                                'validity'    => strtotime($params['validity'][$k]),
                                'status'      => '0',
                                'type'        => 2,
                                'createtime'  => time(),
                                'updatetime'  => time(),
                            ];
                        }

                        $serial_code = [];
                        if(isset($params['period_period'][$k]) && $goods['serial']==1 && $set==1){
                            if(empty($params['period_period'][$k])){
                                $this->error(__('Please enter the product serial number'));
                            }
                            $period_period = unserialize($params['period_period'][$k]);
                            foreach ($period_period as $period_period_k=>$period_period_v){
                                $serial_code[] = $period_period_v['serial'];
                            }
                            $where = [];
                            $where['serial'] = ['in',$serial_code];
                            if(!empty($params['purchase_id'][$k])){
                                $where['purchase_code'] = $params['purchase_id'][$k];
                            }
                            $goods_serial = Db::name('kuyou_goods_serial')->where($where)->column('serial');
                            if(empty($goods_serial)){
                                $this->error(__('The product serial number does not exist'));
                            }
                            foreach ($serial_code as $serial_code_k=>$serial_code_v){
                                if(!in_array($serial_code_v,$goods_serial)){
                                    $this->error($serial_code_v.__('The product serial number does not exist'));
                                }
                            }
                            $goods_serials = Db::name('kuyou_goods_serial')->where(['status'=>['in',[2,3]],'serial'=>['in',$serial_code],'purchase_exchange_id'=>['not in',$info_id]])->column('serial');
                            if(!empty($goods_serials)){
                                $goods_serials = implode(',',$goods_serials);
                                $this->error(__('Product serial number').$goods_serials.'已经待出库，不可重复出库');
                            }
                        }

                        if(!empty($params['info_id'][$k])){
                            $unit_id_order = Db::name('kuyou_goods_sales_order_info')->where(['id'=>$params['info_id'][$k]])->value('goods_unit');
                            if(!empty($unit_id_order) && $unit_id_order!=$params['goods_unit'][$k]){
                                $this->error('选择的商品单位与源购货订单不一致，保存失败');
                            }
                        }

                        $data[] = [
                            'purchase_code'     =>  $params['purchase_code'],
                            'purchase_type'     =>  $params['purchase_type'],
                            'purchase_time'     =>  strtotime($params['purchase_time']),
                            'customers_id'      =>  (int)$params['customers_id'],//客户
                            'sale_user'         =>  (int)$params['sale_user'],//销售员
                            'receiving_address' =>  $params['receiving_address'],

                            'goods_code'        =>  $v,//商品信息
                            'goods_note'        =>  $params['goods_note'][$k],
                            'goods_unit'        =>  (int)$params['goods_unit'][$k],

                            'stores_id'         =>  (int)$params['stores_id'][$k],
                            'allow_stock'       =>  (float)$params['allow_stock'][$k],
                            'order_info_id'     =>  (float)$params['info_id'][$k],
                            'number'            =>  (float)$params['number'][$k],
                            'default_unit'      =>  (float)$params['default_unit'][$k],
                            'basic_nmber'       =>  (float)$params['basic_nmber'][$k],
                            'unit_price'        =>  (float)$params['unit_price'][$k],
                            'cost_price'        =>  0,
                            'discount'          =>  (float)$params['discount'][$k],
                            'dis_money'         =>  (float)$params['dis_money'][$k],
                            'all_money'         =>  (float)$params['all_money'][$k],
                            'remark'            =>  $params['remark'][$k],

                            'goods_attr'        =>  (float)$params['goods_attr'][$k],
                            'tax_included'      =>  (float)$params['tax_included'][$k],
                            'tax_rate'          =>  (float)$params['tax_rate'][$k],
                            'tax_money'         =>  (float)$params['tax_money'][$k],
                            'money_tax_price'   =>  (float)$params['money_tax_price'][$k],

                            'batch_id'          => isset($batch_data['id'])?$batch_data['id']:'',
                            'batch_name'        => isset($batch_data['batch_name'])?$batch_data['batch_name']:'',
                            'manufacture'       => strtotime($params['manufacture'][$k]),
                            'guarantee'         => $params['guarantee'][$k],
                            'validity'          => strtotime($params['validity'][$k]),

                            'preferential_rate' =>  (float)$params['preferential_rate'],
                            'preferential_money'=>  (float)$params['preferential_money'],
                            'real_money'        =>  (float)$params['real_money'],
                            'purchase_money'    =>  (float)$params['purchase_money'][$k],
                            'preparation_id'    =>  $admin['id'],
                            'operation_id'      =>  $admin['id'],

                            'pay_status'        =>  isset($pay_status)?$pay_status:'0',
                            'pay_money'         =>  isset($pay_money)?$pay_money:0,
                            'account'           =>  isset($pay_status)?$params['account']:0,

                            'createtime'        => $row['createtime'],
                            'updatetime'        => time(),
                            'period'            =>  $params['period_period'][$k]===''?null:$params['period_period'][$k],
                            'serial_code'       => $serial_code,
                            'goods_batch_info'  => $goods_batch_info,
                            'user_order_info_id'     => isset($params['user_order_info_id'][$k])?$params['user_order_info_id'][$k]:'',
                        ];
                    }
                }
                $preferential_money = (float)$params['preferential_money'];
                if(!empty($data)){
                    if($all_now_pay_money>0){//当次支付金额
                        if($all_now_pay_money+$preferential_money-$money_tax_price>=0){
                            $pay_status = 1;
                            $pay_money = $money_tax_price-$preferential_money;
                        }else{
                            $pay_status = 2;
                            $pay_money = $all_now_pay_money;
                        }
                    }
                    $rowdata = [
                        'purchase_code'     =>  $params['purchase_code'],
                        'purchase_type'     =>  $params['purchase_type'],
                        'purchase_time'     =>  strtotime($params['purchase_time']),
                        'customers_id'      =>  (int)$params['customers_id'],//客户
                        'sale_user'         =>  (int)$params['sale_user'],//销售员
                        'receiving_address' =>  $params['receiving_address'],
                        'customers_money'   =>  (float)$params['customers_money'],

                        'number'            =>  $number,
                        'all_money'         =>  $all_money,

                        'tax_money'         =>  $tax_money,//税
                        'money_tax_price'   =>  $money_tax_price,//价税合计

                        'preferential_rate' =>  (float)$params['preferential_rate'],
                        'preferential_money'=>  (float)$params['preferential_money'],
                        'real_money'        =>  (float)$params['real_money'],
                        'purchase_money'    =>  (float)$params['costs'],
                        'note'              =>  $params['note'],
                        'preparation_id'    =>  $admin['id'],
                        'operation_id'      =>  $admin['id'],

                        'arrears'           =>  $params['arrears'],
                        'all_arrears'       =>  $params['add_arrears'],

                        'pay_status'        =>  isset($pay_status)?$pay_status:'0',
                        'pay_money'         =>  isset($pay_money)?$pay_money:0,
                        'account'           =>  isset($pay_status)?$params['account']:0,
                        'cost_data'         =>  isset($params['cost_data'])?$params['cost_data']:null,
                        'updatetime'        => time(),
                    ];
                }

                if($this->model->where(['purchase_code'=>trim($params['purchase_code']),'id'=>['<>',$ids]])->find()){
                    $this->error(__('The document number is already occupied'));
                }
                $info_id = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$row['purchase_code']])->column('id');

                //附件记录开始
                $enclosureLog = [];
                if(isset($params['enclosure']) && !empty($params['enclosure'])){//选择了原始单据
                    $enclosure = explode(',',$params['enclosure']);
                    $enclosurelist = Db::name('kuyou_enclosure')->where(['id'=>['in',$params['enclosure']]])->select();
                    if(sizeof($enclosurelist)!=sizeof($enclosure)){
                        $this->error('选择的原始单据有误，请重新选择');
                    }else{
                        foreach ($enclosure as $k=>$v){
                            $enclosureLog[] = [
                                'enclosure_id'  => $v,
                                'purchase_code' => $params['purchase_code'],
                                'type'          => '5',
                            ];
                        }
                    }
                    unset($params['enclosure']);
                }
                //附件记录结束

                $result = false;
                Db::startTrans();
                try {
                    if($examine!=1 && $row['examine_status']==1){
                        $this->examine($ids,2);
                    }

                    Db::name('kuyou_user_ordersaleinfo')->where(['sales_id'=>$ids])->delete();

                    //添加附件记录
                    Db::name('kuyou_enclosure_log')->where(['order_id'=>$ids,'type'=>'5'])->delete();
                    if(!empty($enclosureLog)){
                        foreach ($enclosureLog as $enclosureLog_k=>$enclosureLog_v){
                            $enclosureLog[$enclosureLog_k]['order_id'] = $ids;
                        }
                        Db::name('kuyou_enclosure_log')->insertAll($enclosureLog);
                    }
                    //添加附件记录结束

                    $result = $this->model->where(['id'=>$ids])->update($rowdata);
                    if(!empty($info_id)){
                        Db::name('kuyou_goods_saleord_info')->where(['purchase_id'=>['in',$info_id]])->delete();
                    }

                    $purchase_exchange_id = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$old_purchase_code])->column('id');
                    Db::name('kuyou_goods_serial')->where(['sale_id'=>['in',$purchase_exchange_id],'status'=>'3'])->update([
                        'status'=>'1',
                    ]);
                    Db::name('kuyou_goods_batch_sale_info')->where(['purchase_info_id'=>['in',$purchase_exchange_id]])->delete();

                    Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$row['purchase_code']])->delete();

                    foreach ($data as $data_k=>$data_v){
                        $goods_serial = $data_v['serial_code'];
                        unset($data_v['serial_code']);

                        $goods_batch_info = $data_v['goods_batch_info'];
                        unset($data_v['goods_batch_info']);

                        $user_order_info_id = $data_v['user_order_info_id'];unset($data_v['user_order_info_id']);
                        $purchase_id = Db::name('kuyou_goods_sales_info')->insertGetId($data_v);
                        if(!empty($user_order_info_id) && !empty($params['order_id'])){
                            Db::name('kuyou_user_ordersaleinfo')->insert([
                                'sales_id' => $ids,
                                'orderinfo_id' => $user_order_info_id,
                                'salesinfo_id' => $purchase_id,
                            ]);
                        }

                        //批次
                        if($goods_batch_info){
                            $goods_batch_info['purchase_info_id'] = $purchase_id;
                            Db::name('kuyou_goods_batch_sale_info')->insert($goods_batch_info);
                        }

                        if(!empty($goods_serial)){
                            Db::name('kuyou_goods_serial')->where(['serial'=>['in',$goods_serial]])->update([
                                'status'=>'3',
                                'sale_id' => $purchase_id,
                                'sale_code' => $data_v['purchase_code']
                            ]);
                        }

                        if(!empty($data_v['order_info_id'])){
                            Db::name('kuyou_goods_saleord_info')->insert([
                                'order_id'=>$data_v['order_info_id'],
                                'purchase_id'=>$purchase_id,
                                'number'=>$data_v['number'],
                                'money'=>$data_v['pay_money'],
                                'all_money'=>$data_v['all_money'],
                                'tax_rate'=>$data_v['tax_rate'],
                                'tax_money'=>$data_v['tax_money'],
                                'money_tax_price'=>$data_v['money_tax_price']
                            ]);
                        }
                    }
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    if($examine!=1){
                        try {
                            $exa = $this->examine($ids,1);
                        } catch (Exception $e) {
                        }
                        if(!isset($exa) || $exa['status']!=1){
                            $this->error(__('No rows were inserted'));
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $row['order_id'] = Db::name('kuyou_user_ordersale')->where(['sales_id'=>$row['id']])->value('order_id');
        if(!empty($row['order_id'])){
            $this->assignconfig("receiving_address", $row['receiving_address']);
        }
        $this->view->assign("row", $row);

        $purchase_info = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$row['purchase_code']])->select();
        foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
            $purchase_info[$purchase_info_k]['serial'] = $purchase_info_v['period'];
            $purchase_info[$purchase_info_k]['unit'] = json_decode($this->getUnit($purchase_info_v['goods_code']),true);
            $purchase_info[$purchase_info_k]['user_order_info_id'] = Db::name('kuyou_user_ordersaleinfo')->where(['salesinfo_id'=>$purchase_info_v['id']])->value('orderinfo_id');
        }
        $this->view->assign("purchase_info", $purchase_info);

        $give_account = Db::name('kuyou_customers')->where(['id'=>$row['customers_id']])->value('need_receive_account');
        if($row['examine_status']==1){
            $this->assignconfig("give_account", $give_account-$row['arrears']);
            $this->view->assign("give_account", (float)$give_account);
        }else{
            $this->assignconfig("give_account", $give_account);
            $this->view->assign("give_account", (float)$give_account+$row['arrears']);
        }

        //查询原始单据
        $enclosure_id = Db::name('kuyou_enclosure_log')->where(['order_id'=>$ids,'type'=>'5'])->column('enclosure_id');
        if(!empty($enclosure_id)){
            $this->assign('enclosure_count',sizeof($enclosure_id));
            $enclosure_id = implode(',',$enclosure_id);
        }else{
            $enclosure_id = '';
        }
        $this->assign('enclosure_ids',$enclosure_id);

        $kuyou_customers = Db::name('kuyou_customers')->where(['id'=>$row['customers_id']])->field('id,customer_name')->find();
        $this->assignconfig("kuyou_customers", $kuyou_customers);

        //审核禁止输入
        $examine_set= Db::name('kuyou_sets')->where(['name'=>'examine'])->value('is_checked');
        if($examine_set==1 && $row['examine_status']==1){
            $this->assign('exclass','hiddens');
        }
        $this->assignconfig("examine_status", $row['examine_status']);
        $this->assignconfig("suppliersList", $this->model->getSuppliersList());
        $this->assignconfig("expendTypeList", $this->model->getExpendTypeList());

        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            $examine= Db::name('kuyou_sets')->where(['name'=>'examine'])->value('is_checked');
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $goods_purchase_id = [];$goods_purchase_code = [];
            foreach ($list as $list_k=>$list_v){
                if($list_v['examine_status']==1){
                    (new \app\admin\controller\kuyou\Checkout())->checkOutTime($list_v['purchase_time']);
                }
                if($list_v['check_status']==1){
                    $this->error(__('Cannot delete data that has already been processed'));
                }
                if($list_v['examine_status']==1 && $examine==1){
                    $this->error(__('Cannot delete audited data'));
                }elseif ($list_v['examine_status']==1 && $examine!=1){
                    try {
                        $this->examine($list_v['id'],2);
                    } catch (Exception $e) {
                    }
                }
                if(!in_array($list_v['id'],$goods_purchase_id)){
                    $goods_purchase_id[] = $list_v['id'];
                }
                if(!in_array($list_v['purchase_code'],$goods_purchase_code)){
                    $goods_purchase_code[] = $list_v['purchase_code'];
                }

                $order_id = Db::name('kuyou_user_ordersale')->where(['sales_id'=>$list_v['id']])->value('order_id');
                if($order_id){
                    $order_status = Db::name('kuyou_user_order')->where(['id'=>$order_id])->value('status');
                    if($order_status>2){
                        $this->error(__('Cannot delete data of purchase orders associated with shipments').$list_v['purchase_code']);
                    }
                }
            }
            $goods_purchase_info = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>['in',$goods_purchase_code]])->column('id');

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                    $exchange_info = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$v['purchase_code']])->column('id');
                    Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$v['purchase_code']])->delete();
                    Db::name('kuyou_goods_serial')->where(['sale_id'=>['in',$exchange_info]])->update(['status'=>'1']);

                    Db::name('kuyou_user_ordersale')->where(['sales_id'=>$ids])->delete();
                    Db::name('kuyou_user_ordersaleinfo')->where(['sales_id'=>$ids])->delete();
                }
                Db::name('kuyou_goods_sales_summary')->where(['purchase_code'=>['in',$goods_purchase_code]])->delete();
                if(!empty($goods_purchase_id)){//销货订单关联
                    Db::name('kuyou_goods_saleord')->where(['purchase_id'=>['in',$goods_purchase_id]])->delete();
                    Db::name('kuyou_goods_batch_sale_info')->where(['purchase_id'=>['in',$goods_purchase_id]])->delete();
                }
                if(!empty($goods_purchase_info)){//销货订单详情关联
                    Db::name('kuyou_goods_saleord_info')->where(['purchase_id'=>['in',$goods_purchase_info]])->delete();
                }

                Db::name('kuyou_goods_salethird')->where([
                    'purchase_id' => $ids,
                ])->delete();

                //删除附件记录
                Db::name('kuyou_enclosure_log')->where(['purchase_code'=>$v['purchase_code'],'type'=>'5'])->delete();
                Db::commit();
            }catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    //审核/反审核
    public function examine($ids = null,$examine=1){
        $examine_set= Db::name('kuyou_sets')->where(['name'=>'examine'])->value('is_checked');
        if(empty($ids)){
            if($examine_set!=1){
                return ['status'=>0,'msg'=>__('System error')];
            }
            $this->error(__('Please select the data to be operated on'));
        }
        if($examine==1){
            $list = $this->model->where(['id'=>['in',$ids],'examine_status'=>'0'])->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuyou\Checkout())->checkOutTime($list_v['purchase_time']);
            }
            $set_discount = \think\Db::name('kuyou_sets')->where(['name'=>'discount'])->value('is_checked');

            $purchase_code = $this->model->where(['id'=>['in',$ids],'examine_status'=>'0'])->column('purchase_code');
            if(empty($list)){
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>__('System error')];
                }
                $this->error(__('Please select the data that needs to be reviewed'));
            }
            $account_list_obj = Db::name('kuyou_accounts')->field('id,account_coding,account_name,balance')->select();
            $account_list = [];
            foreach ($account_list_obj as $account_list_obj_k=>$account_list_obj_v){
                $account_list[$account_list_obj_v['id']] = $account_list_obj_v;
            }
            $examine_ids = [];
            $accountData = [];
            $arrears = [];
            $log = [];
            $payment_log = [];$invoicing = [];
            foreach ($list as $list_k=>$list_v){
                $examine_ids[] = $list_v['id'];
                if($list_v['arrears']>0){
                    $suppliers = Db::name('kuyou_customers')->where(['id'=>$list_v['customers_id']])->field('id,need_receive_account')->find();
                    if($suppliers){
                        $status = false;
                        foreach ($arrears as $arrears_k=>$arrears_v){
                            if($arrears_v['id']==$suppliers['id']){
                                $arrears[$arrears_k]['need_receive_account'] = $list_v['arrears']+$arrears_v['need_receive_account'];
                                $status = true;
                            }
                        }
                        if(!$status){
                            $arrears[] = [
                                'id'  => $suppliers['id'],
                                'need_receive_account'  => $list_v['arrears']+$suppliers['need_receive_account'],
                            ];
                        }
                    }
                }
                if($list_v['pay_money']>0){
                    $accountData[] = [
                        'balance'  => $list_v['pay_money'],
                        'account'  => $list_v['account'],
                    ];
                    $log[] = [
                        'account_coding'    => $account_list[$list_v['account']]['account_coding'],
                        'account_name'      => $account_list[$list_v['account']]['account_name'],
                        'balance'           => $account_list[$list_v['account']]['balance']-$list_v['pay_money'],
                        'income'            => $list_v['pay_money'],
                        'pay_time'          => $list_v['purchase_time'],
                        'bill_code'         => $list_v['purchase_code'],
                        'business_type'     => 5,
                        'customers_id'      => $list_v['customers_id'],
                        'remark'            => $list_v['note'],
                        'createtime'        => time(),
                        'updatetime'        => time(),
                    ];
                }
                $payment_log[] = [
                    'customers_id'   => $list_v['customers_id'],
                    'sale_user'      => $list_v['sale_user'],
                    'business_type'  => 1,
                    'bill_time'      => $list_v['purchase_time'],
                    'bill_code'      => $list_v['purchase_code'],
                    'bill_codes'     => $list_v['purchase_code'],
                    'purchase_money' => $list_v['all_money'],
                    'discount_money' => $list_v['preferential_money'],
                    'real_money'     => $list_v['real_money'],
                    'money_tax_price'=> $list_v['money_tax_price'],
                    'pay_money'      => $list_v['pay_money'],
                    'give_money'     => $list_v['arrears'],
                    'reamrk'         => $list_v['note'],
                    'createtime'     => time(),
                ];
                if($set_discount==1){
                    $money = $list_v['real_money'];
                }else{
                    $money = $list_v['money_tax_price'];
                }
                $invoicing[] = [
                    'customers_id'   => $list_v['customers_id'],
                    'status'         => $list_v['pay_status'],
                    'bill_time'      => $list_v['purchase_time'],
                    'bill_code'      => $list_v['purchase_code'],
                    'bill_codes'     => $list_v['purchase_code'],
                    'money'          => $list_v['real_money'],
                    'tax'            => $list_v['tax_money'],
                    'real_money'     => $money,
                    'pay_money'      => 0,
                    'give_money'     => $money,
                    'createtime'     => time(),
                ];
            }
            $editstock = [];$addstock = [];$goods_stock = [];
            $info_list = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>['in',$purchase_code]])->select();
            foreach ($info_list as $info_list_k=>$info_list_v){
                try {
                    unset($info_list[$info_list_k]['period']);
                } catch (Exception $e) {
                }
            }

            $purchase_info_id = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>['in',$purchase_code]])->column('id');

            $purchase_info = [];
            $goods_stock_log = [];//收发详情
            $serial_log = [];//序列号跟踪
            foreach ($info_list as $list_k=>$list_v){
                //序列号跟踪
                $goods_serial = Db::name('kuyou_goods_serial')->where((new Goods())->setWhere(['sale_id'=>$list_v['id']]))->field('goods_id,serial,stores_id,attribute_id')->select();
                foreach ($goods_serial as $goods_serial_k=>$goods_serial_v){
                    $serial_log[] = (new Goods())->setDataInfo([
                        'goods_id'=>$goods_serial_v['goods_id'],
                        'stores_id'=>$goods_serial_v['stores_id'],
                        'attribute_id'=>$goods_serial_v['attribute_id'],
                        'serial'=>$goods_serial_v['serial'],
                        'business_type'=>'4',
                        'store_status'=>'2',
                        'remarks' => $list_v['remark'],
                        'purchase_id'=>$list_v['id'],
                        'customer_id'=>$list_v['customers_id'],
                        'purchase_code'=>$list_v['purchase_code'],
                        'purchase_time' => $list_v['purchase_time'],
                        'createtime'  => time(),
                        'updatetime'  => time(),
                    ]);
                }
                //序列号跟踪

                $info_list[$list_k]['sales_order_id']    = $list_v['id'];
                $info_list[$list_k]['createtime']        = time();
                $info_list[$list_k]['updatetime']        = time();
                $stock_number = $list_v['basic_nmber'];
                $goods = Db::name('kuyou_goods')->where(['goods_code'=>$list_v['goods_code']])->field('id,attribute_id,inventory,unit_id')->find();
                $status = false;
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['id']==$goods['id']){
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']-$stock_number;
                        $status = true;
                    }
                }
                if(!$status){
                    $goods_stock[] = [
                        'id'=>$goods['id'],
                        'inventory'=>$goods['inventory']-$stock_number,
                    ];
                }
                //商品库存
                $where = [];
                $where['goods_code'] = $list_v['goods_code'];
                $where['stores_id'] = $list_v['stores_id'];
                if($list_v['goods_attr']>0){
                    $goods_attr = $list_v['goods_attr'];
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                }

                //获取成本单价
                $balance_cost=0;$balance_unit=0;
                $goodsInfo = json_decode((new GoodsPurchase())->getUnit($list_v['goods_code'],$list_v['stores_id'],$list_v['goods_attr']),true);
                $unitData = isset($goodsInfo['unit'])?$goodsInfo['unit']:[];
                if(empty($unitData)){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>__('Product information retrieval error')];
                    }
                    $this->error(__('Product information retrieval error'));
                }else{
                    foreach ($unitData as $unitData_k=>$unitData_v){
                        if($unitData_v['id']==$goods['unit_id']){
                            $balance_unit = $unitData_v['purchase'];
                        }
                    }
                }
                //获取成本单价结束

                $stock = Db::name('kuyou_goods_stock')->where($where)->field('stock,id')->find();
                if($stock){
                    $status = false;
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['id']==$stock['id']){
                            $editstock[$editstock_k]['stock'] -= $stock_number;
                            $status = true;
                            $balance_cost   = (round($balance_unit*$stock_number,4));
                        }
                    }
                    if(!$status){
                        $editstock[] = [
                            'id'=>$stock['id'],
                            'stock'=>(round($stock['stock']-$stock_number,4))
                        ];
                        $balance_cost   = (round($balance_unit*$stock_number,4));
                    }
                }else{
                    $store = Db::name('kuyou_stores')->where(['id'=>$list_v['stores_id']])->value('name');
                    if($examine!=1){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败'];
                        }
                        return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败'];
                    }
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>__('commodity').$store.__('Insufficient inventory')];
                    }
                    $this->error(__('commodity').$store.__('Insufficient inventory'));
                }
                $purchase_info[] = $list_v['id'];

                //收发详情
                if($goods){
                    $goods_stock_log[] = (new Goods())->setDataInfo([
                        'purchase_code' => $list_v['purchase_code'],
                        'purchase_id'   => $list_v['id'],
                        'purchase_time' => $list_v['purchase_time'],
                        'business_type' => '4',
                        'customer_id'   => $list_v['customers_id'],
                        'goods_id'      => $goods['id'],
                        'goods_code'    => $list_v['goods_code'],
                        'attribute_id'  => (float)$list_v['goods_attr'],
                        'goods_unit'    => (float)$list_v['goods_unit'],
                        'stores_id'     => (float)$list_v['stores_id'],
                        'remark'        => $list_v['remark'],
                        'out_number'    => (float)$list_v['number'],
                        'out_basics'    => (float)$list_v['basic_nmber'],
                        'out_unit'      => $balance_unit,
                        'out_cost'      => $balance_cost,
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ]);
                }
                //收发详情结束
            }
            foreach ($editstock as $editstock_k=>$editstock_v){
                if($editstock_v['stock']<0){
                    $store = Db::name('kuyou_stores')->where(['id'=>$list_v['stores_id']])->value('name');
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败'];
                    }
                    $this->error($store.'库存不足,操作失败');
                }
            }

            $order_count = [];
            $code_id = Db::name('kuyou_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->column('order_id');
            $purchase_order_info = Db::name('kuyou_goods_sales_order_info')
                ->field('id,purchase_code')
                ->where(['id'=>['in',$code_id]])
                ->select();
            foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){
                $purord_info = Db::name('kuyou_goods_saleord_info')
                    ->where(['order_id'=>$purchase_order_info_v['id'],'purchase_id'=>['in',$purchase_info]])->field('sum(number) as number,sum(money) as money,sum(all_money) as all_money')->find();
                $purchase_order_info[$purchase_order_info_k]['pay_count'] = isset($purord_info['number'])?$purord_info['number']:0;
                $purchase_order_info[$purchase_order_info_k]['pay_money'] = isset($purord_info['money'])?$purord_info['money']:0;
                $purchase_order_info[$purchase_order_info_k]['all_money'] = isset($purord_info['all_money'])?$purord_info['all_money']:0;
            }
            foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){
                $status = false;
                foreach ($order_count as $order_count_k=>$order_count_v){
                    if($order_count_v['purchase_code']==$purchase_order_info_v['purchase_code']){
                        $order_count[$order_count_k]['store_number'] += $purchase_order_info_v['pay_count'];
                        $status = true;
                    }
                }
                if(!$status){
                    $order_count[] = [
                        'purchase_code'  => $purchase_order_info_v['purchase_code'],
                        'store_number'  => $purchase_order_info_v['pay_count'],
                    ];
                }
            }
            //修改状态
            if(!empty($order_count)){
                foreach ($order_count as $order_count_k=>$order_count_v){
                    $number = Db::name('kuyou_goods_sales_order')->where(['purchase_code'=>$order_count_v['purchase_code']])->field('id,number,store_number')->find();
                    if(($number['number']-$number['store_number'])<=$order_count_v['store_number']){
                        $order_count[$order_count_k]['status'] = 1;
                    }else{
                        $order_count[$order_count_k]['status'] = 2;
                    }
                    $order_count[$order_count_k]['id'] = $number['id'];
                }
            }

            //批次库存数据
            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $goods_serial = Db::name('kuyou_goods_sales_info')->where(['id'=>['in',$purchase_info_id]])->select();

            $goods_batch_add = [];$goods_batch_edit = [];$goods_batch_log = [];
            foreach ($goods_serial as $k=>$v){
                $goods = Db::name('kuyou_goods')->where((new Goods())->setWhere(['goods_code'=>$v['goods_code']]))->field('id,goods_name,attribute_id,inventory,unit_id,batch')->find();
                if($goods['batch']==1){
                    //先通过单位计算需修改多单位商品的库存数量
                    if($goods['unit_id']==$v['goods_unit']){
                        $stock_number = $v['number'];
                    }else{
                        $unit_conversion = Db::name('kuyou_unit_conversion')->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$v['goods_unit']]))->value('advance_rate');
                        if(empty($unit_conversion)){
                            if($examine_set!=1){
                                return ['status'=>0,'msg'=>__('Failed to query product unit information')];
                            }
                            $this->error(__('Failed to query product unit information'));
                        }else{
                            $stock_number = round(($v['number']*$unit_conversion),$number_decimal);
                        }
                    }

                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if($v['goods_attr']){
                        $where['attribute_id'] = $v['goods_attr'];
                    }
                    $where['stores_id'] = $v['stores_id'];
                    $where['batch_id'] = $v['batch_id'];
                    $where = (new Goods())->setWhere($where);
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){
                        $status = false;
                        foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){
                            if($goods_batch_add_v['batch_id']==$v['batch_id'] && $goods_batch_add_v['goods_id']==$v['goods_id'] && $goods_batch_add_v['attribute_id']==$v['attribute_id'] && $goods_batch_add_v['stores_id']==$v['stores_id']){
                                $goods_batch_add[$goods_batch_add_k]['stock'] -= $stock_number;
                                $status = true;
                            }
                        }
                        if(!$status){
                            $goods_batch_add[] = (new Goods())->setDataInfo([
                                'batch_id' => $v['batch_id'],
                                'goods_id' => $goods['id'],
                                'goods_code' => $v['goods_code'],
                                'attribute_id' => $v['goods_attr'],
                                'stores_id' => $v['stores_id'],
                                'stock' => $stock_number,
                                'createtime' => time(),
                                'updatetime' => time(),
                            ]);
                        }
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']-$stock_number;
                        }
                    }
                    foreach ($goods_batch_edit as $k2=>$v2){
                        if($v2<0){
                            if($examine_set!=1){
                                return ['status'=>0,'msg'=>__('commodity').$goods['goods_name'].__('Insufficient inventory')];
                            }
                            $this->error(__('commodity').$goods['goods_name'].__('Insufficient inventory'));
                        }
                    }

                    //商品批次跟踪
                    $goods_batch_log[] = (new Goods())->setDataInfo([
                        'goods_id'      => $goods['id'],
                        'goods_unit'    => $v['goods_unit'],
                        'stores_id'     => $v['stores_id'],
                        'batch_id'      => (float)$v['batch_id'],
                        'number'        => (float)$v['number'],
                        'purchase_code' => $v['purchase_code'],
                        'purchase_time' => $v['purchase_time'],
                        'business_type' => '4',
                        'remarks'       => $v['remark'],
                        'order_id'      => $v['id'],
                        'unit_price'    => $v['unit_price'],
                        'tax_included'  => $v['tax_included'],
                        'all_money'     => $v['all_money'],
                        'tax_money'     => $v['tax_money'],
                        'money_tax_price' => $v['money_tax_price'],
                        'customer_id'   => $v['customers_id'],
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ]);
                }
                //商品批次跟踪结束
            }
            //批次库存数据结束

            //应收账款明细表
            $ex_list = $this->model->where(['id'=>['in',$ids],'examine_status'=>'0','arrears'=>['>','0']])->select();
            $accounts_receivable = [];
            foreach ($ex_list as $ex_list_k=>$ex_list_v){
                $accounts_receivable[] = (new Goods())->setDataInfo([
                    'customers_id'      => $ex_list_v['customers_id'],
                    'purchase_time'     => $ex_list_v['purchase_time'],
                    'purchase_code'     => $ex_list_v['purchase_code'],
                    'business_type'     => '6',
                    'receivable'        => $ex_list_v['arrears'],
                    'staff_member_id'   => $ex_list_v['sale_user'],
                    'remark'            => $ex_list_v['note'],
                    'sale_money'        => $ex_list_v['money_tax_price'],
                    'receivable_money'  => round(($ex_list_v['real_money']+$ex_list_v['customers_money']),4),
                    'amount'            => $ex_list_v['preferential_money'],
                    'cost'              => $ex_list_v['customers_money'],
                    'real_money'        => $ex_list_v['pay_money'],
                    'createtime'        => time(),
                    'updatetime'        => time(),
                ]);
            }

            $admin = Session::get('admin');

            //采购销售费用
            $cost_data = [];
            foreach ($list as $list_k=>$list_v){
                if(!empty($list_v['cost_data'])){
                    $cost_datas = unserialize($list_v['cost_data']);
                    foreach ($cost_datas as $cost_datas_k=>$cost_datas_v){
                        $list_v['supplyer_code'] = isset($list_v['supplyer_code'])?$list_v['supplyer_code']:'';
                        $cost_data[] = (new Goods())->setDataInfo([
                            'purchase_time'     => time(),
                            'purchase_code'     => (new Goods())->getCodes('',27),
                            'business_type'     => '2',
                            'suppliers_ids'     => isset($list_v['customers_id'])?$list_v['customers_id']:null,
                            'suppliers_id'      => $cost_datas_v['suppliers_id'],
                            'expend_type_id'    => $cost_datas_v['expend_type_id'],
                            'purchase_number'   => $list_v['purchase_code'],
                            'purchase_times'    => $list_v['purchase_time'],
                            'money'             => $cost_datas_v['money'],
                            'pay_money'         => $cost_datas_v['money'],
                            'remark'            => $cost_datas_v['remark'],
                            'preparation_id'    => $admin['id'],
                            'operation_id'      => $admin['id'],
                            'createtime'        => time(),
                            'updatetime'        => time(),
                        ]);
                    }
                }
            }

            //销售利润表
            $profit = [];
            foreach ($list as $list_k=>$list_v){
                $amount = null;
                $amounts = Db::name('kuyou_goods_sales_info')->where((new Goods())->setWhere(['purchase_code'=>$list_v['purchase_code']]))->sum('dis_money');
                if($amounts>0){
                    $amount = $amounts;
                }
                $profit[] = (new Goods())->setDataInfo([
                    'purchase_type' => '1',
                    'purchase_code' => $list_v['purchase_code'],
                    'purchase_time' => $list_v['purchase_time'],
                    'customers_id'  => $list_v['customers_id'],
                    'sale_user'     => $list_v['sale_user'],
                    'preparation_id'=> $list_v['preparation_id'],
                    'examine_id'    => $admin['id'],
                    'number'        => $list_v['number'],
                    'amount'        => $amount,
                    'all_money'     => $list_v['all_money'],
                    'tax_money'     => $list_v['tax_money'],
                    'money_tax_price'=> $list_v['money_tax_price'],
                    'preferential_money'=> $list_v['preferential_money'],
                    'purchase_money'=> $list_v['purchase_money'],
                    'real_money'    => $list_v['real_money'],
                    'customers_money'=> $list_v['customers_money'],
                    'need_money'    => $list_v['real_money']+$list_v['customers_money'],
                    'pay_money'     => $list_v['pay_money'],
                    'remark'        => $list_v['note'],
                ]);
            }

            //核销单源单
            $writeoff_source = [];
            foreach ($list as $list_k=>$list_v){
                if($list_v['arrears']>0){
                    $writeoff_source[] = [
                        'purchase_type' => 3,
                        'type' => 1,
                        'customers_id' => $list_v['customers_id'],
                        'purchase_code' => $list_v['purchase_code'],
                        'purchase_time' => $list_v['purchase_time'],
                        'price' => $list_v['real_money'],
                        'money' => $list_v['pay_money'],
                        'write_off' => $list_v['arrears'],
                        'remark' => $list_v['note'],
                    ];
                }
            }

            Db::startTrans();
            try {

                $orderinfo_id = Db::name('kuyou_user_ordersaleinfo')->where(['sales_id'=>['in',$ids]])->column('orderinfo_id');
                $orderinfo = Db::name('kuyou_user_orderinfo')->where(['id'=>['in',$orderinfo_id]])->field('goods_id,number')->select();
                foreach ($orderinfo as $orderinfo_k=>$orderinfo_v){
                    Db::name('kuyou_goods')->where(['id'=>$orderinfo_v['goods_id']])->inc('sale_number',$orderinfo_v['number'])->update();
                }

                if(!empty($writeoff_source)){
                    Db::name('kuyou_writeoff_source')->insertAll($writeoff_source);
                }

                if(!empty($profit)){
                    Db::name('kuyou_goods_sales_profit')->insertAll($profit);
                }
                if(!empty($cost_data)){
                    Db::name('kuyou_cost')->insertAll($cost_data);
                }
                if(!empty($accounts_receivable)){
                    Db::name('kuyou_accounts_receivable')->insertAll($accounts_receivable);
                }

                //序列号跟踪
                if(!empty($serial_log)){
                    Db::name('kuyou_goods_serial_log')->insertAll($serial_log);
                }
                if(!empty($goods_stock_log)){
                    Db::name('kuyou_goods_log')->insertAll($goods_stock_log);
                }
                //批次库存数据
                if(!empty($goods_batch_log)){
                    Db::name('kuyou_goods_batch_log')->insertAll($goods_batch_log);
                }
                if(!empty($goods_batch_add)){
                    Db::name('kuyou_goods_batch_stock')->insertAll($goods_batch_add);
                }
                if(!empty($goods_batch_edit)){
                    foreach ($goods_batch_edit as $k=>$v){
                        Db::name('kuyou_goods_batch_stock')->where((new Goods())->setWhere(['id'=>$k]))->update(['stock'=>$v]);
                    }
                }
                //批次库存数据结束

                if(!empty($purchase_info_id)){
                    Db::name('kuyou_goods_serial')->where(['sale_id'=>['in',$purchase_info_id]])->update(['store_status'=>'2']);
                    Db::name('kuyou_goods_batch_sale_info')->where(['purchase_info_id'=>['in',$purchase_info_id]])->update(['status'=>'1']);
                }
                if(!empty($order_count)){
                    $goodsSalesOrder = (new \app\admin\model\kuyou\GoodsSalesOrder());
                    foreach ($order_count as $order_count_k=>$order_count_v){
                        $goodsSalesOrder->where(['id'=>$order_count_v['id']])->inc('store_number',$order_count_v['store_number'])->update();
                        unset($order_count[$order_count_k]['store_number']);
                    }
                    $goodsSalesOrder->saveAll($order_count);
                }
                if(!empty($payment_log)){
                    (new \app\admin\model\kuyou\SalesPaymentLog())->saveAll($payment_log);
                }
                if(!empty($invoicing)){
                    (new \app\admin\model\kuyou\SalesInvoicing())->saveAll($invoicing);
                }
                $result = $this->model->where(['id'=>['in',$ids],'examine_status'=>'0'])->update(['examine_status'=>1,'examine_id'=>$admin['id'],'examine_time'=>time(),'operation_id'=>$admin['id']]);
                if(!empty($log)){
                    Db::name('kuyou_accounts_log')->insertAll($log);
                }
                if(!empty($editstock)){
                    (new \app\admin\model\kuyou\GoodsStock())->saveAll($editstock);
                }
                if(!empty($goods_stock)){
                    (new \app\admin\model\kuyou\Goods())->saveAll($goods_stock);
                }
                if(!empty($arrears)){
                    (new \app\admin\model\kuyou\Customers())->saveAll($arrears);
                }
                Db::name('kuyou_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->update(['status'=>'1','times'=>time()]);
                foreach ($info_list as $info_list_k=>$info_list_v){
                    if(isset($info_list_v['id'])){
                        unset($info_list[$info_list_k]['id']);
                    }
                }
                Db::name('kuyou_goods_sales_summary')->insertAll($info_list);
                if(!empty($accountData)){
                    foreach ($accountData as $accountData_k=>$accountData_v){
                        Db::name('kuyou_accounts')->where(['id'=>$accountData_v['account']])->inc('balance',$accountData_v['balance'])->update();
                    }
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if(isset($result) && $result !== false){
                if($examine_set!=1){
                    return ['status'=>1,'msg'=>__('Operation successful')];
                }
                $this->success(sizeof($list).__('The data review was successful'));
            }else{
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>__('operation failed')];
                }
                $this->error(__('Audit failed'));
            }
        }else{
            $plist = $list = $this->model->where(['id'=>['in',$ids],'examine_status'=>'1'])->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuyou\Checkout())->checkOutTime($list_v['purchase_time']);
                if($list_v['check_status']==1){
                    $this->error(__('Cannot reverse audit data that has already been processed'));
                }
                $purchase_id = Db::name('kuyou_collection_sale')->where(['purchase_number'=>$list_v['purchase_code']])->find();
                if(!empty($purchase_id)){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>__('Cannot manipulate data of generated payment receipts')];
                    }
                    $this->error(__('Cannot reverse audit data of generated payment receipts'));
                }
                $purchase_id = Db::name('kuyou_writeoff_info')->where(['purchase_number'=>$list_v['purchase_code']])->find();
                if(!empty($purchase_id)){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>__('Cannot manipulate data of generated write off documents')];
                    }
                    $this->error(__('Cannot reverse audit data of generated write off forms'));
                }
            }
            if(empty($list)){
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>__('operation failed')];
                }
                $this->error(__('Please select the data that needs to be de audited'));
            }
            foreach ($list as $list_k=>$list_v){
                $purchase_id = Db::name('kuyou_goods_saleexc')->where(['purchase_id'=>$list_v['id'],'type'=>'2'])->column('exchange_id');
                if(!empty($purchase_id)){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>__('Cannot manipulate data of generated return orders')];
                    }
                    $this->error(__('Cannot reverse audit data of generated return orders'));
                }
                $order_id = Db::name('kuyou_user_ordersale')->where(['sales_id'=>$list_v['id']])->value('order_id');
                if($order_id){
                    $order_status = Db::name('kuyou_user_order')->where(['id'=>$order_id])->value('status');
                    if($order_status>2){
                        $this->error(__('Cannot reverse audit the data of purchase orders associated with shipments').$list_v['purchase_code']);
                    }
                }
            }
            $arrears = [];
            foreach ($list as $list_k=>$list_v){
                if($list_v['arrears']>0){
                    $suppliers = Db::name('kuyou_customers')->where(['id'=>$list_v['customers_id']])->field('id,need_receive_account')->find();
                    if($suppliers){
                        $status = false;
                        foreach ($arrears as $arrears_k=>$arrears_v){
                            if($arrears_v['id']==$suppliers['id']){
                                $arrears[$arrears_k]['need_receive_account'] = $arrears_v['need_receive_account']-$list_v['arrears'];
                                $status = true;
                            }
                        }
                        if(!$status){
                            $arrears[] = [
                                'id'  => $suppliers['id'],
                                'need_receive_account'  => $suppliers['need_receive_account']-$list_v['arrears'],
                            ];
                        }
                    }
                }
            }
            $list = $this->model->where(['id'=>['in',$ids],'examine_status'=>'1'])->column('purchase_code');

            $account_list_obj = Db::name('kuyou_accounts')->field('id,account_coding,account_name,balance')->select();
            $account_list = [];
            foreach ($account_list_obj as $account_list_obj_k=>$account_list_obj_v){
                $account_list[$account_list_obj_v['id']] = $account_list_obj_v;
            }

            $log = Db::name('kuyou_accounts_log')->where(['bill_code'=>['in',$list],'business_type'=>'5'])->select();
            $accountData = [];
            foreach ($log as $list_k=>$list_v){
                $accountData[] = [
                    'balance'  => $list_v['income'],
                    'account'  => $list_v['account_coding'],
                ];
            }

            $editstock = [];$goods_stock = [];
            $info_list = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>['in',$list]])->select();

            $purchase_info_id = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>['in',$list]])->column('id');

            $purchase_info = [];
            foreach ($info_list as $list_k=>$list_v){
                $info_list[$list_k]['createtime']        = time();
                $info_list[$list_k]['updatetime']        = time();
                $stock_number = $list_v['basic_nmber'];
                $goods = Db::name('kuyou_goods')->where(['goods_code'=>$list_v['goods_code']])->field('id,attribute_id,inventory,goods_name')->find();
                $status = false;
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['id']==$goods['id']){
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']+$stock_number;
                        $status = true;
                    }
                }
                if(!$status){
                    $goods_stock[] = [
                        'id'=>$goods['id'],
                        'inventory'=>$goods['inventory']+$stock_number,
                    ];
                }
                //商品库存
                $where = [];
                $where['goods_code'] = $list_v['goods_code'];
                $where['stores_id'] = $list_v['stores_id'];
                if($list_v['goods_attr']>0){
                    $goods_attr = $list_v['goods_attr'];
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                }
                $stock = Db::name('kuyou_goods_stock')->where($where)->field('stock,id')->find();
                if($stock){
                    $status = false;
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['id']==$stock['id']){
                            $editstock[$editstock_k]['stock'] += $stock_number;
                            $status = true;
                        }
                    }
                    if(!$status){
                        $editstock[] = [
                            'id'=>$stock['id'],
                            'stock'=>(round($stock['stock']+$stock_number,2))
                        ];
                    }
                }else{
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'系统数据错误，操作失败'];
                    }
                    $this->error('系统数据错误,反审核失败');
                }
                $purchase_info[] = $list_v['id'];
            }

            $order_count = [];
            $code_id = Db::name('kuyou_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->column('order_id');
            $purchase_order_info = Db::name('kuyou_goods_sales_order_info')
                ->field('id,purchase_code')
                ->where(['id'=>['in',$code_id]])
                ->select();
            foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){
                $purord_info = Db::name('kuyou_goods_saleord_info')
                    ->where(['order_id'=>$purchase_order_info_v['id'],'purchase_id'=>['in',$purchase_info]])->field('sum(number) as number,sum(money) as money,sum(all_money) as all_money')->find();
                $purchase_order_info[$purchase_order_info_k]['pay_count'] = isset($purord_info['number'])?$purord_info['number']:0;
                $purchase_order_info[$purchase_order_info_k]['pay_money'] = isset($purord_info['money'])?$purord_info['money']:0;
                $purchase_order_info[$purchase_order_info_k]['all_money'] = isset($purord_info['all_money'])?$purord_info['all_money']:0;
            }
            foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){
                $status = false;
                foreach ($order_count as $order_count_k=>$order_count_v){
                    if($order_count_v['purchase_code']==$purchase_order_info_v['purchase_code']){
                        $order_count[$order_count_k]['store_number'] += $purchase_order_info_v['pay_count'];
                        $status = true;
                    }
                }
                if(!$status){
                    $order_count[] = [
                        'purchase_code'  => $purchase_order_info_v['purchase_code'],
                        'store_number'  => $purchase_order_info_v['pay_count'],
                    ];
                }
            }
            //修改状态
            if(!empty($order_count)){
                foreach ($order_count as $order_count_k=>$order_count_v){
                    $number = Db::name('kuyou_goods_sales_order')->where(['purchase_code'=>$order_count_v['purchase_code']])->field('id,number,store_number')->find();
                    if($number['store_number']<=$order_count_v['store_number']){
                        $order_count[$order_count_k]['status'] = 0;
                    }else{
                        $order_count[$order_count_k]['status'] = 2;
                    }
                    $order_count[$order_count_k]['id'] = $number['id'];
                }
            }

            //批次库存数据
            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $goods_serial = Db::name('kuyou_goods_sales_info')->where(['id'=>['in',$purchase_info_id]])->select();

            $goods_batch_add = [];$goods_batch_edit = [];
            foreach ($goods_serial as $k=>$v){
                $goods = Db::name('kuyou_goods')->where((new Goods())->setWhere(['goods_code'=>$v['goods_code']]))->field('id,goods_name,attribute_id,inventory,unit_id,batch')->find();
                if($goods['batch']==1){
                    //先通过单位计算需修改多单位商品的库存数量
                    if($goods['unit_id']==$v['goods_unit']){
                        $stock_number = $v['number'];
                    }else{
                        $unit_conversion = Db::name('kuyou_unit_conversion')->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$v['goods_unit']]))->value('advance_rate');
                        if(empty($unit_conversion)){
                            if($examine_set!=1){
                                return ['status'=>0,'msg'=>__('Failed to query product unit information')];
                            }
                            $this->error(__('Failed to query product unit information'));
                        }else{
                            $stock_number = round(($v['number']*$unit_conversion),$number_decimal);
                        }
                    }

                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if($v['goods_attr']){
                        $where['attribute_id'] = $v['goods_attr'];
                    }
                    $where['stores_id'] = $v['stores_id'];
                    $where['batch_id'] = $v['batch_id'];
                    $where = (new Goods())->setWhere($where);
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){
                        $status = false;
                        foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){
                            if($goods_batch_add_v['batch_id']==$v['batch_id'] && $goods_batch_add_v['goods_id']==$goods['id'] && $goods_batch_add_v['attribute_id']==$v['attribute_id'] && $goods_batch_add_v['stores_id']==$v['stores_id']){
                                $goods_batch_add[$goods_batch_add_k]['stock'] += $stock_number;
                                $status = true;
                            }
                        }
                        if(!$status){
                            $goods_batch_add[] = (new Goods())->setDataInfo([
                                'batch_id' => $v['batch_id'],
                                'goods_id' => $goods['id'],
                                'goods_code' => $v['goods_code'],
                                'attribute_id' => $v['goods_attr'],
                                'stores_id' => $v['stores_id'],
                                'stock' => $stock_number,
                                'createtime' => time(),
                                'updatetime' => time(),
                            ]);
                        }
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] += $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']+$stock_number;
                        }
                    }
                }
            }
            //批次库存数据结束

            //应收账款明细表
            $ex_list = $this->model->where(['id'=>['in',$ids],'examine_status'=>'1'])->column('purchase_code');

            $admin = Session::get('admin');
            Db::startTrans();
            try {

                $orderinfo_id = Db::name('kuyou_user_ordersaleinfo')->where(['sales_id'=>['in',$ids]])->column('orderinfo_id');
                $orderinfo = Db::name('kuyou_user_orderinfo')->where(['id'=>['in',$orderinfo_id]])->field('goods_id,number')->select();
                foreach ($orderinfo as $orderinfo_k=>$orderinfo_v){
                    Db::name('kuyou_goods')->where(['id'=>$orderinfo_v['goods_id']])->dec('sale_number',$orderinfo_v['number'])->update();
                }

                //销售利润表
                Db::name('kuyou_writeoff_source')->where(['purchase_code'=>['in',$list],'purchase_type'=>'3','type'=>'1'])->delete();

                foreach ($plist as $plist_k=>$plist_v){
                    Db::name('kuyou_goods_sales_profit')->where((new Goods())->setWhere(['purchase_type'=>'1','purchase_code'=>$plist_v['purchase_code']]))->delete();
                }
                Db::name('kuyou_cost')->where((new Goods())->setWhere(['business_type'=>'2','purchase_number'=>['in',$ex_list]]))->delete();
                Db::name('kuyou_accounts_receivable')->where((new Goods())->setWhere(['business_type'=>'6','purchase_code'=>['in',$ex_list]]))->delete();

                //序列号跟踪
                if(!empty($purchase_info_id)){
                    Db::name('kuyou_goods_serial_log')->where((new Goods())->setWhere(['business_type'=>'4','purchase_id'=>['in',$purchase_info_id]]))->delete();
                }
                //收发详情
                if(!empty($purchase_info_id)){
                    Db::name('kuyou_goods_log')->where((new Goods())->setWhere(['business_type'=>'4','purchase_id'=>['in',$purchase_info_id]]))->delete();
                }
                //批次库存数据
                //删除批次跟踪
                if(!empty($purchase_info_id)){
                    Db::name('kuyou_goods_batch_log')->where((new Goods())->setWhere(['business_type'=>'4','order_id'=>['in',$purchase_info_id]]))->delete();
                }
                if(!empty($goods_batch_edit)){
                    foreach ($goods_batch_edit as $k=>$v){
                        Db::name('kuyou_goods_batch_stock')->where((new Goods())->setWhere(['id'=>$k]))->update(['stock'=>$v]);
                    }
                }
                //批次库存数据结束

                if(!empty($purchase_info_id)){
                    Db::name('kuyou_goods_serial')->where(['sale_id'=>['in',$purchase_info_id]])->update(['store_status'=>'1']);
                    Db::name('kuyou_goods_batch_sale_info')->where(['purchase_info_id'=>['in',$purchase_info_id]])->update(['status'=>'0']);
                }
                if(!empty($order_count)){
                    $goodsSalesOrder = (new \app\admin\model\kuyou\GoodsSalesOrder());
                    foreach ($order_count as $order_count_k=>$order_count_v){
                        $goodsSalesOrder->where(['id'=>$order_count_v['id']])->dec('store_number',$order_count_v['store_number'])->update();
                        unset($order_count[$order_count_k]['store_number']);
                    }
                    $goodsSalesOrder->saveAll($order_count);
                }
                (new \app\admin\model\kuyou\SalesPaymentLog())->where(['bill_codes'=>['in',$list]])->delete();
                (new \app\admin\model\kuyou\SalesInvoicing())->where(['bill_codes'=>['in',$list]])->delete();

                $result = $this->model->where(['id'=>['in',$ids],'examine_status'=>'1'])->update(['examine_status'=>0,'examine_id'=>0,'examine_time'=>'','operation_id'=>$admin['id']]);
                Db::name('kuyou_accounts_log')->where(['bill_code'=>['in',$list],'business_type'=>5])->delete();
                Db::name('kuyou_goods_sales_summary')->where(['purchase_code'=>['in',$list]])->delete();
                if(!empty($accountData)){
                    foreach ($accountData as $accountData_k=>$accountData_v){
                        Db::name('kuyou_accounts')->where(['account_coding'=>$accountData_v['account']])->dec('balance',$accountData_v['balance'])->update();
                    }
                }
                if(!empty($editstock)){
                    (new \app\admin\model\kuyou\GoodsStock())->saveAll($editstock);
                }
                if(!empty($goods_stock)){
                    (new \app\admin\model\kuyou\Goods())->saveAll($goods_stock);
                }
                if(!empty($arrears)){
                    (new \app\admin\model\kuyou\Customers())->saveAll($arrears);
                }
                Db::name('kuyou_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->update(['status'=>'0']);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if(isset($result) && $result !== false){
                if($examine_set!=1){
                    return ['status'=>1,'msg'=>__('Operation successful')];
                }
                $this->success(sizeof($list).__('The data has been successfully de audited'));
            }else{
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>__('operation failed')];
                }
                $this->error(__('Anti audit failed'));
            }
        }
    }

    //对单/反对单
    public function counter($ids = null,$counter=1){
        if($counter==1){
            $list = $this->model->where(['id'=>['in',$ids],'check_status'=>'0'])->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuyou\Checkout())->checkOutTime($list_v['purchase_time']);
            }
            if(empty($list)){
                $this->error(__('Please select the data required for the order'));
            }
            $admin = Session::get('admin');
            Db::startTrans();
            try {
                $result = $this->model->where(['id'=>['in',$ids],'check_status'=>'0'])->update(['check_status'=>1,'operation_id'=>$admin['id']]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if(isset($result) && $result !== false){
                $this->success(sizeof($list).__('Data pairing successful'));
            }else{
                $this->error(__('Single failure'));
            }
        }else{
            $list = $this->model->where(['id'=>['in',$ids],'check_status'=>'1'])->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuyou\Checkout())->checkOutTime($list_v['purchase_time']);
            }
            if(empty($list)){
                $this->error(__('Please select the data that needs to be opposed to the order'));
            }
            $admin = Session::get('admin');
            Db::startTrans();
            try {
                $result = $this->model->where(['id'=>['in',$ids],'check_status'=>'1'])->update(['check_status'=>'0','operation_id'=>$admin['id']]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if(isset($result) && $result !== false){
                $this->success(sizeof($list).__('The data opposition order was successful'));
            }else{
                $this->error(__('Oppose single failed'));
            }
        }
    }

    //检测可生成退货单
    public function checkPurchase(){
        $order_id = isset($_POST['exchange'])?trim($_POST['exchange']):0;
        if(empty($order_id)){
            $this->error(__('Parameter error'));
        }
        $row = Db::name('kuyou_goods_sales')->where(['id'=>$order_id])
            ->field('examine_status,purchase_code')
            ->find();
        if(empty($row)){
            $this->error(__('Parameter error'));
        }
        if($row['examine_status']!=1){
            $this->error('此购货单未审核，不可生成退货单');
        }
        $purchase_info = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$row['purchase_code']])->select();
        foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
            $purord_info = Db::name('kuyou_goods_saleexc_info')->where(['purchase_id'=>$purchase_info_v['id'],'type'=>'2'])->field('sum(number) as number,sum(money) as money')->select();
            $gp_number = isset($purord_info[0]['number'])?$purord_info[0]['number']:0;
            if($purchase_info_v['number']-$gp_number<=0){
                unset($purchase_info[$purchase_info_k]);
            }
        }
        if(empty($purchase_info)){
            $this->error(__('This sales order has all generated return orders'));
        }
        $this->success();
    }
    //获取应付款余额
    public function getSuppliers($customers_id){
        if(empty($customers_id)){
            return 0;
        }else{
            $give_account = Db::name('kuyou_customers')->where(['id'=>$customers_id])->value('need_receive_account');
            return (float)$give_account;
        }
    }
    //获取单位信息
    public function getUnits($goods_code='',$store=''){
        if($goods_code){
            $row = Db::name('kuyou_goods')->where(['goods_code'=>$goods_code])->field('id,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,warehousing,warehouse')->find();
            $goods_id = isset($row['id'])?$row['id']:0;
        }else{
            $goods_id = $_POST['goods_id'];
            $row = Db::name('kuyou_goods')->where(['id'=>$goods_id])->field('unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,warehousing,warehouse')->find();
        }
        if(empty($row)){
            return [];
        }
        if($store){
            $stock = Db::name('kuyou_goods_stock')->where(['goods_code'=>$row['goods_code'],'stores_id'=>$store])->value('stock');
        }else{
            $stock = Db::name('kuyou_goods_stock')->where(['goods_code'=>$row['goods_code'],'stores_id'=>$row['manager_warehouse_id']])->value('stock');
        }
        if($row['more_unit']==1){
            $unit_ids_arr = Db::name('kuyou_unit_conversion')->where(['id'=>$row['more_unit_id']])->field('main_unit,vice_unit,advance_rate')->find();
            $unit_ids = [$unit_ids_arr['main_unit'],$unit_ids_arr['vice_unit']];
            $goods_price = Db::name('kuyou_goods_price')->where(['unit'=>['in',$unit_ids],'goods_id'=>$goods_id])->select();
        }else{
            $unit_ids = [$row['unit_id']];
        }
        $unit = Db::name('kuyou_unit')->where(['id'=>['in',$unit_ids]])->field('id,name')->select();
        foreach ($unit as $unit_k=>$unit_v){
            if(isset($goods_price)){
                foreach ($goods_price as $goods_price_k=>$goods_price_v){
                    if($unit_v['id']==$goods_price_v['unit']){
                        $unit[$unit_k]['is_default'] = $goods_price_v['is_default'];
                        if($unit_v['id']==$unit_ids_arr['vice_unit']){
                            $unit[$unit_k]['purchase'] = round(($goods_price_v['purchase']/$unit_ids_arr['advance_rate']),2);
                            $unit[$unit_k]['basic_nmber'] = $unit_ids_arr['advance_rate'];
                        }else{
                            $unit[$unit_k]['purchase'] = $goods_price_v['purchase'];
                            $unit[$unit_k]['basic_nmber'] = 1;
                        }
                    }
                }
            }else{
                if($unit_v['id']==$row['unit_id']){
                    $unit[$unit_k]['is_default'] = 1;
                    $unit[$unit_k]['basic_nmber'] = 1;
                    $unit[$unit_k]['purchase'] = $row['purchase'];
                }
            }
        }
        $data = [];
        foreach ($unit_ids as $unit_ids_k=>$unit_ids_v){
            foreach ($unit as $unit_k=>$unit_v){
                if($unit_ids_v==$unit_v['id']){
                    if($unit_v['is_default']==1){
                        $unit_v['stock'] = is_null($stock)?0:$stock;
                    }else{
                        $unit_v['stock'] = round($stock/$unit_v['basic_nmber'],2);
                    }
                    $data[] = $unit_v;
                }
            }
        }
        return json_encode($data);
    }

    //获取单位价格信息
    public function getUnit($goods_code='',$store='',$attr='',$customers_id=''){
        $taxes= Db::name('kuyou_sets')->where(['name'=>'price_taxes'])->value('is_checked');

        if($goods_code){
            $row = Db::name('kuyou_goods')->where(['goods_code'=>$goods_code])
                ->field('market_sell_price,wholesale_price,vip_price,discount,discount2,batch,serial,id,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life,warehousing,warehouse')
                ->find();
            $goods_id = isset($row['id'])?$row['id']:0;
        }else{
            $goods_id = isset($_POST['goods_id'])?$_POST['goods_id']:0;
            $row = Db::name('kuyou_goods')->where(['id'=>$goods_id])
                ->field('market_sell_price,wholesale_price,vip_price,discount,discount2,batch,serial,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life,warehousing,warehouse')
                ->find();
        }
        if(empty($row)){
            return [];
        }

        //获取设置
        $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
        $setList = [];
        foreach ($set as $set_k=>$set_v){
            if(!empty($set_v['set'])){
                $set_v['set'] = unserialize($set_v['set']);
            }
            $setList[$set_v['name']] = $set_v;
        }
        //数量价格格式化
        $number_decimal = $setList['number_decimal']['value'];
        $price_decimal  = $setList['price_decimal']['value'];

        $field = 'market_sell_price';
        if($customers_id){
            $customers = Db::name('kuyou_customers')->where(['id'=>$customers_id])->find();
            if($customers){
                switch ($customers['customerlevel_id']){
                    case 1:
                        $field = 'market_sell_price';
                        break;
                    case 2:
                        $field = 'wholesale_price';
                        break;
                    case 3:
                        $field = 'vip_price';
                        break;
                    case 4:
                        $field = 'discount';
                        break;
                    case 5:
                        $field = 'discount2';
                        break;
                    default:
                        break;
                }
            }
        }

        $auxiliary = Db::name('kuyou_auxiliary')->where(['status'=>1])->column('name','id');
        $goods_attr = Db::name('kuyou_goods_attr')->where(['goods_id'=>$goods_id])->select();
        foreach ($goods_attr as $goods_attr_k=>$goods_attr_v){
            $aux_combination = Db::name('kuyou_aux_combination')->where(['id'=>$goods_attr_v['attribute_id']])->value('attr_ids');
            $goods_attr[$goods_attr_k]['attr_ids'] = [];
            $goods_attr[$goods_attr_k]['attr_name'] = '';
            if($aux_combination){
                $goods_attr[$goods_attr_k]['attr_id'] = $aux_combination;
                $aux_combination = explode('&',$aux_combination);
                $goods_attr[$goods_attr_k]['attr_ids'] = $aux_combination;
                foreach ($aux_combination as $aux_combination_k=>$aux_combination_v){
                    $aux_combination[$aux_combination_k] = $auxiliary[$aux_combination_v];
                }
                $aux_combination = implode('/',$aux_combination);
                $goods_attr[$goods_attr_k]['attr_name'] = $aux_combination;
            }
        }


        $where = [];
        $where['goods_id'] = $goods_id;
        if($store){
            $where['stores_id'] = $store;
        }else{
            $where['stores_id'] = $row['manager_warehouse_id'];
        }
        if($attr){
            //$attribute_id = Db::name('kuyou_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->value('attribute_id');
            $where['attribute_id'] = $attr;
        }
        $stock = Db::name('kuyou_goods_stock')->where($where)->value('stock');
        if($row['more_unit']==1){
            $unit = Db::name('kuyou_unit_conversion')->alias('uc')
                ->join('kuyou_unit u','u.id=uc.vice_unit')
                ->where(['uc.main_unit'=>$row['unit_id']])
                ->order('uc.advance_rate','ASC')
                ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                ->select();
            $unit_ids = [$row['unit_id']];
            foreach ($unit as $unit_k=>$unit_v){
                $unit_ids[] = $unit_v['id'];
            }
            $goods_price = Db::name('kuyou_goods_price')->where(['unit'=>['in',$unit_ids],'goods_id'=>$goods_id])->select();
            foreach ($goods_price as $goods_price_k=>$goods_price_v){
                foreach ($unit as $unit_k=>$unit_v){
                    if($goods_price_v['unit']==$row['unit_id']){
                        $goods_price[$goods_price_k]['advance_rate'] = 1;
                    }else if($goods_price_v['unit']==$unit_v['id']){
                        $goods_price[$goods_price_k]['advance_rate'] = $unit_v['advance_rate'];
                    }
                }
            }
        }else{
            $unit_ids = [$row['unit_id']];
        }

        $unit = Db::name('kuyou_unit')->where(['id'=>['in',$unit_ids]])->field('id,name')->select();
        foreach ($unit as $unit_k=>$unit_v){
            if(isset($goods_price)){
                foreach ($goods_price as $goods_price_k=>$goods_price_v){
                    if($unit_v['id']==$goods_price_v['unit']){
                        $unit[$unit_k]['is_default'] = $goods_price_v['is_default'];
                        if($field=='discount' || $field=='discount2'){
                            $unit[$unit_k]['purchase'] = round( ($goods_price_v['market_sell_price']*$goods_price_v[$field]/100),$price_decimal );
                        }else{
                            $unit[$unit_k]['purchase'] = $goods_price_v[$field];
                        }
                        if($goods_price_v['is_default']!=1){
                            $unit[$unit_k]['basic_nmber'] = $goods_price_v['advance_rate'];
                        }else{
                            $unit[$unit_k]['basic_nmber'] = 1;
                        }
                    }
                }
            }else{
                if($row['auxiliary']==1 && !empty($attr)){
                    $purchase_data = Db::name('kuyou_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->find();
                    if($field=='discount' || $field=='discount2'){
                        $purchase = round( ($purchase_data['market_sell_price']*$purchase_data[$field]/100),$price_decimal );
                    }else{
                        $purchase = $purchase_data[$field];
                    }
                }else{
                    if($field=='discount' || $field=='discount2'){
                        $purchase = round( ($row['market_sell_price']*$row[$field]/100),$price_decimal );
                    }else{
                        $purchase = $row[$field];
                    }
                }
                if($unit_v['id']==$row['unit_id']){
                    $unit[$unit_k]['is_default'] = 1;
                    $unit[$unit_k]['basic_nmber'] = 1;
                    $unit[$unit_k]['purchase'] = $purchase;
                }
            }
            $unit[$unit_k]['purchase'] = isset($unit[$unit_k]['purchase'])?$unit[$unit_k]['purchase']:0;
            $unit[$unit_k]['basic_nmber'] = isset($unit[$unit_k]['basic_nmber'])?$unit[$unit_k]['basic_nmber']:1;
            $unit[$unit_k]['is_default'] = isset($unit[$unit_k]['is_default'])?$unit[$unit_k]['is_default']:2;
        }

        $set= Db::name('kuyou_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();
        foreach ($unit as $unit_k=>$unit_v){
            $tax = $set['value'];
            if(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1){
                $unit[$unit_k]['tax'] = 1;
                $unit[$unit_k]['tax_rate'] = $tax;
            }else{
                $unit[$unit_k]['tax'] = 0;
                $unit[$unit_k]['tax_rate'] = 0;
            }
            if(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1 && $taxes==1){
                $unit[$unit_k]['purchase'] = round(($unit_v['purchase']/(100+$tax)*100),4);
                $unit[$unit_k]['tax_included'] = round(($unit_v['purchase']),4);
            }elseif(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1 && $taxes!=1){
                $unit[$unit_k]['purchase'] = round(($unit_v['purchase']),4);
                $unit[$unit_k]['tax_included'] = round(($unit_v['purchase']*(100+$tax)/100),4);
            }else{
                $unit[$unit_k]['purchase'] = round(($unit_v['purchase']),4);
                $unit[$unit_k]['tax_included'] = round(($unit_v['purchase']),4);
            }
        }
        $data = [];
        foreach ($unit_ids as $unit_ids_k=>$unit_ids_v){
            foreach ($unit as $unit_k=>$unit_v){
                if($unit_ids_v==$unit_v['id']){
                    if($unit_v['is_default']==1){
                        $unit_v['stock'] = is_null($stock)?0:$stock;
                    }else{
                        $unit_v['stock'] = round($stock/$unit_v['basic_nmber'],2);
                    }
                    $unit_v['tax_money'] = round(($unit_v['tax_included']-$unit_v['purchase']),4);
                    $data[] = $unit_v;
                }
            }
        }
        return json_encode(['unit'=>$data,'attr'=>$goods_attr,'goods'=>$row]);
    }

    public function formatInfo2(){
        $data = input();

        $row = Db::name('kuyou_goods')->where(['goods_code'=>$data['goods_code']])->field('batch,serial,id,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id')->find();
        if(empty($row)){
            return [];
        }else{
            $goods_unit = $data['goods_unit'];
            if($row['more_unit']==1){//多单位，多属性以单位为准
                $goods_price = Db::name('kuyou_goods_price')->where(['goods_code'=>$data['goods_code'],'unit'=>$goods_unit])->find();
            }elseif ($row['more_unit']!=1 && $row['auxiliary']==1){//单一单位，多属性以属性价格为准
                $goods_price = Db::name('kuyou_goods_attr')->where(['id'=>$data['attr']])->find();
            }
        }

        //获取设置
        $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
        $setList = [];
        foreach ($set as $set_k=>$set_v){
            if(!empty($set_v['set'])){
                $set_v['set'] = unserialize($set_v['set']);
            }
            $setList[$set_v['name']] = $set_v;
        }
        //数量价格格式化
        $number_decimal = $setList['number_decimal']['value'];
        $price_decimal  = $setList['price_decimal']['value'];

        //税率
        $taxes= Db::name('kuyou_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();
        if(!isset($data['tax_rate']) || (isset($data['tax_rate']) && $data['tax_rate']==='')){
            $data['tax_rate'] = $taxes['value'];
        }
        $taxes_status = Db::name('kuyou_sets')->where(['name'=>'taxes'])->value('is_checked');//是否启用税金
        if($taxes_status!=1){
            $data['tax_rate'] = 0;
            $data['tax_money'] = 0;
            $data['tax_included'] = $data['unit_price'];
            $data['money_tax_price'] = $data['all_money'];
        }

        //需格式化字段数组
        $format = [
            'number'=>$number_decimal,
            'allow_stock'=>$number_decimal,
            'basic_nmber'=>$number_decimal,
            'unit_price'=>$price_decimal,
            'tax_included'=>$price_decimal,
            'discount'=>$price_decimal,
            'dis_money'=>$price_decimal,
            'all_money'=>$price_decimal,
            'tax_rate'=>$price_decimal,
            'tax_money'=>$price_decimal,
            'money_tax_price'=>$price_decimal,
            'advance_rate'=>$number_decimal,
        ];
        foreach ($data as $k=>$v){
            if(array_key_exists($k,$format)){
                $data[$k] = (float)$data[$k];
            }
        }

        $field = 'market_sell_price';
        $customers_id = isset($data['customers_id'])?$data['customers_id']:'';
        if($customers_id){
            $customers = Db::name('kuyou_customers')->where(['id'=>$customers_id])->find();
            if($customers){
                switch ($customers['customerlevel_id']){
                    case 1:
                        $field = 'market_sell_price';
                        break;
                    case 2:
                        $field = 'wholesale_price';
                        break;
                    case 3:
                        $field = 'vip_price';
                        break;
                    case 4:
                        $field = 'discount';
                        break;
                    case 5:
                        $field = 'discount2';
                        break;
                    default:
                        break;
                }
            }
        }

        $data['tax_included'] = isset($data['tax_included'])?$data['tax_included']:$data['unit_price']*(100+$data['tax_rate'])/100;
        $data['tax_rate'] = isset($data['tax_rate'])?$data['tax_rate']:0;
        $data['discount'] = isset($data['discount'])?$data['discount']:0;
        $data['dis_money'] = isset($data['dis_money'])?$data['dis_money']:0;

        $taxes= Db::name('kuyou_sets')->where(['name'=>'price_taxes'])->value('is_checked');//价格含税
        $now_field = $data['now_field'];
        switch ($now_field){
            case 'goods_unit':
                //商品单位
                if($taxes){
                    $data['tax_included'] = isset($goods_price[$field])?$goods_price[$field]:$data['tax_included'];//含税单价
                    $data['unit_price'] = $data['tax_included']*100/(100+$data['tax_rate']);//单价
                }else{
                    $data['unit_price'] = isset($goods_price[$field])?$goods_price[$field]:$data['unit_price'];//单价
                    $data['tax_included'] = $data['unit_price']*(100+$data['tax_rate'])/100;//含税单价
                }
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'attr':
                //商品属性
                if($taxes){
                    $data['tax_included'] = isset($goods_price[$field])?$goods_price[$field]:$data['tax_included'];//含税单价
                    $data['unit_price'] = $data['tax_included']*100/(100+$data['tax_rate']);//单价
                }else{
                    $data['unit_price'] = isset($goods_price[$field])?$goods_price[$field]:$data['unit_price'];//单价
                    $data['tax_included'] = $data['unit_price']*(100+$data['tax_rate'])/100;//含税单价
                }
                $data['unit_price'] = $data['tax_included']*100/(100+$data['tax_rate']);//单价
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'number':
                //数量
                $data['unit_price'] = $data['tax_included']*100/(100+$data['tax_rate']);//单价
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'unit_price':
                //单价
                $data['tax_included'] = $data['unit_price']*(100+$data['tax_rate'])/100;//含税单价
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'tax_included':
                //含税单价
                $data['unit_price'] = $data['tax_included']*100/(100+$data['tax_rate']);//单价
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'tax_rate':
                //税率
                $data['tax_included'] = $data['unit_price']*(100+$data['tax_rate'])/100;//含税单价
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'tax_money':
                //税额
                $data['money_tax_price'] = $data['all_money'] + $data['tax_money'];//价税合计减折扣额
                break;
            case 'discount':
                //折扣率
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'dis_money':
                //折扣额
                $data['money_tax_price'] = $data['tax_included']*$data['number'];//价税合计未减折扣额
                $data['discount'] = (100*$data['dis_money']+$data['dis_money']*$data['tax_rate'])/$data['money_tax_price'];//折扣率
                $data['money_tax_price'] = $data['money_tax_price'] - ($data['money_tax_price'] * ($data['discount'])/100);//价税合计减折扣额
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                break;
            case 'all_money':
                //购货金额
                $data['money_tax_price'] = $data['all_money']*(100+$data['tax_rate'])/100;//价税合计
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                $money_tax_price = $data['money_tax_price']*(100+$data['discount'])/100;//价税合计未减折扣额
                $data['tax_included'] = $money_tax_price/$data['number'];//含税单价
                $data['unit_price'] = $data['tax_included']*100/(100+$data['tax_rate']);//单价
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                break;
            case 'money_tax_price':
                //价税合计
                $data['all_money'] = $data['money_tax_price']*100/(100+$data['tax_rate']);//购货金额
                $data['tax_money'] = $data['all_money']*$data['tax_rate']/100;//税
                $money_tax_price = $data['money_tax_price']*(100+$data['discount'])/100;//价税合计未减折扣额
                $data['tax_included'] = $money_tax_price/$data['number'];//含税单价
                $data['unit_price'] = $data['tax_included']*100/(100+$data['tax_rate']);//单价
                $data['dis_money'] = $data['money_tax_price']/(100+$data['tax_rate'])*$data['discount'];//折扣额
                break;
            default:
                return [];
                break;
        }

        $data['advance_rate'] = 1;
        if($row['more_unit']==1){
            $advance_rate = Db::name('kuyou_unit_conversion')->where(['main_unit'=>$row['more_unit_id'],'vice_unit'=>$goods_price['unit']])->value('advance_rate');
            if(!empty($advance_rate)){
                $data['advance_rate'] = $advance_rate;
            }
        }
        $data['basic_nmber'] = $data['number']*$data['advance_rate'];
        foreach ($data as $data_k=>$data_v){
            if(array_key_exists($data_k,$format)){
                $data[$data_k] = round($data[$data_k],$format[$data_k]);
            }
        }

        if(isset($data['tax_rate'])){
            $data['tax_money'] = $data['money_tax_price']-$data['all_money'];
        }else{
            $data['tax_money'] = 0;
        }

        $arr = [];
        foreach ($data as $data_k=>$data_v){
            if(array_key_exists($data_k,$format)){
                $data[$data_k] = round($data[$data_k],$format[$data_k]);

                if($format[$data_k]>0){
                    $price = explode('.',$data[$data_k]);
                    if(isset($price[1])){
                        $cha = $format[$data_k]-strlen($price[1]);
                    }else{
                        $cha = $format[$data_k];
                        $data[$data_k] = $data[$data_k].'.';
                    }
                    $str = '';
                    for ($a=0;$a<$cha;$a++){
                        $str .= '0';
                    }
                    $data[$data_k] .= $str;
                }

            }
            $arr[] = ['key'=>$data_k,'value'=>$data[$data_k]];
        }
        return json_encode(['data'=>$arr]);
    }
    //获取批次
    public function getBranch($goods_code=''){
        $data = input();
        $row = Db::name('kuyou_goods')->where(['goods_code'=>$goods_code])->field('batch,shelf_life')->find();
        $goods_batch = Db::name('kuyou_goods_batch')->where(['goods_code'=>$data['goods_code']])->field('id,batch_name,manufacture,guarantee,validity')->select();
        foreach ($goods_batch as $goods_batch_k=>$goods_batch_v){
            $goods_batch[$goods_batch_k]['manufacture'] = date('Y-m-d',$goods_batch_v['manufacture']);
            $goods_batch[$goods_batch_k]['validity'] = date('Y-m-d',$goods_batch_v['validity']);
        }
        return json_encode(['goods'=>$row,'batch'=>$goods_batch]);
    }
    public function getTime($manufacture='',$shelf_life='',$validity=''){
        if(!empty($manufacture) && !empty($shelf_life)){
            $validity = date('Y-m-d',strtotime($manufacture)+$shelf_life*86400);
        }elseif(!empty($validity) && !empty($shelf_life)){
            $manufacture = date('Y-m-d',strtotime($validity)-$shelf_life*86400);
        }elseif(!empty($validity) && !empty($manufacture)){
            $shelf_life = ceil((strtotime($validity)-strtotime($manufacture))/86400);
        }
        return json_encode(['manufacture'=>$manufacture,'shelf_life'=>$shelf_life,'validity'=>$validity]);
    }
    //获取客户收货地址
    public function getReceivingAddress(){
        $customers_id = isset($_POST['customers_id'])?trim($_POST['customers_id']):0;
        if(empty($customers_id)){
            return '';
        }
        $address = Db::name('kuyou_customers')->where(['id'=>$customers_id])->value('getgoods_address');
        return (empty($address)?'':$address);
    }
}
